ZOJ 3229 Shoot the Bullet 无源汇上下界最大流

简介:
Shoot the Bullet

Time Limit: 2 Seconds       Memory Limit: 32768 KB       Special Judge

Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utopia where humans and other beings such as fairies, youkai(phantoms), and gods live peacefully together. Shameimaru Aya is a crow tengu with the ability to manipulate wind who has been in Gensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns the Bunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautiful danmaku(barrange) or cute girls living inGensokyo. She is the biggest connoisseur of rumors about the girls of Gensokyo among the tengu. Her intelligence gathering abilities are the best in Gensokyo!

During the coming n days, Aya is planning to take many photos of m cute girls living in Gensokyo to write Bunbunmaru News daily and record at least Gx photos of girl x in total in theBunkachou. At the k-th day, there are Ck targets, Tk1Tk2, ..., TkCk. The number of photos of target Tki that Aya takes should be in range [LkiRki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use her last spell card to attack Aya. What's more, Aya cannot take more than Dk photos at the k-th day. Under these constraints, the more photos, the better.

Aya is not good at solving this complex problem. So she comes to you, an earthling, for help.

Input

There are about 40 cases. Process to the end of file.

Each case begins with two integers 1 <= n <= 365, 1 <= m <= 1000. Then m integers, G1G2, ..., Gm in range [0, 10000]. Then n days. Each day begins with two integer 1 <= C <= 100, 0 <= D <= 30000. Then C different targets. Each target is described by three integers, 0 <= T < m, 0 <= L <= R <= 100.

Output

For each case, first output the number of photos Aya can take, -1 if it's impossible to satisfy her needing. If there is a best strategy, output the number of photos of each girl Aya should take at each day on separate lines. The output must be in the same order as the input. If there are more than one best strategy, any one will be OK.

Output a blank line after each case.

Sample Input

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 3 9
1 3 9
2 3 9

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 0 3
1 3 6
2 6 9

2 3
12 12 12
3 15
0 3 9
1 3 9
2 3 9
3 21
0 0 3
1 3 6
2 6 12

Sample Output

36
6
6
6
6
6
6

36
9
6
3
3
6
9

-1

External Links

TeamShanghaiAlice(banner) 
Wikipedia
Touhou Wiki


把每一天看成一个点,每一个女孩也看成一个点,添加源和汇,源向每一天连上[0,d]的边。每一天与每一个女孩假设有拍照任务的话连上[l,c]的边,每一个女孩与汇连上[g,oo]的边,于是构成一个有上下界的图,直接用上下界最大流来搞即可了

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis,pos) memset(vis,pos,sizeof(vis))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;

const int MAXN=2222;

struct Edge{
    int from,to,cap,flow;
};
bool cmp(const Edge& a,const Edge& b){
    return a.from < b.from || (a.from == b.from && a.to < b.to);
}
struct Dinic{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];
    int d[MAXN];
    int cur[MAXN];
    void init(int n){
        this->n=n;
        for(int i=0;i<=n;i++)G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int cap){
        edges.push_back((Edge){from,to,cap,0});
        edges.push_back((Edge){to,from,0,0});//当是无向图时,反向边容量也是cap,有向边时,反向边容量是0
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    bool BFS(){
        CLR(vis,0);
        queue<int> Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty()){
            int x=Q.front();
            Q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge& e=edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int x,int a){
        if(x==t||a==0)return a;
        int flow=0,f;
        for(int& i=cur[x];i<G[x].size();i++){
            Edge& e=edges[G[x][i]];
            if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)break;
            }
        }
        return flow;
    }
    //当所求流量大于need时就退出。减少时间
    int Maxflow(int s,int t,int need){
        this->s=s;this->t=t;
        int flow=0;
        while(BFS()){
            CLR(cur,0);
            flow+=DFS(s,INF);
            if(flow>need)return flow;
        }
        return flow;
    }
    //最小割割边
    vector<int> Mincut(){
        BFS();
        vector<int> ans;
        for(int i=0;i<edges.size();i++){
            Edge& e=edges[i];
            if(vis[e.from]&&!vis[e.to]&&e.cap>0)ans.push_back(i);
        }
        return ans;
    }
    void Reduce(){
        for(int i = 0; i < edges.size(); i++) edges[i].cap -= edges[i].flow;
    }
    void ClearFlow(){
        for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;
    }
};

int w[MAXN],d[MAXN],low[100010];

Dinic solver;

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        solver.init(n+m+3);
        int tmp;
        CLR(w,0),CLR(d,0),CLR(low,0);
        FOR(i,1,m){
            scanf("%d",&tmp);
            w[n+i]-=tmp;
            w[n+m+1]+=tmp;
        }
        int c;
        int g=0;
        FOR(i,1,n){
            scanf("%d%d",&c,&d[i]);
            int t;
            REP(j,c){
                scanf("%d%d%d",&t,&low[++g],&tmp);
                t++;
                solver.AddEdge(i,t+n,tmp-low[g]);
                w[i]-=low[g];
                w[t+n]+=low[g];
            }
        }
        FOR(i,1,n) solver.AddEdge(0,i,d[i]);
        FOR(i,1,m) solver.AddEdge(n+i,n+m+1,INF);
        solver.AddEdge(n+m+1,0,INF);
        int sum=0;
        FOR(i,0,n+m+1){
            if(w[i]>0){
                solver.AddEdge(n+m+2,i,w[i]);
                sum+=w[i];
            }
            if(w[i]<0)
                solver.AddEdge(i,n+m+3,-w[i]);
        }
        int ans=solver.Maxflow(n+m+2,n+m+3,2*INF);
        //cout<<sum<<" "<<ans<<endl;
        if(ans!=sum){
            printf("-1\n");
        }
        else{
            tmp=solver.Maxflow(0,n+m+1,INF);
            cout<<tmp<<endl;
            FOR(i,1,g)
             printf("%d\n",solver.edges[2*i-2].flow+low[i]);
        }
        printf("\n");
    }
    return 0;
}






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5153858.html,如需转载请自行联系原作者
相关文章
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
81 0
HDOJ 1081(ZOJ 1074) To The Max(动态规划)
|
Java C语言
HDOJ(HDU) 2139 Calculate the formula(水题,又一个用JavaAC不了的题目)
HDOJ(HDU) 2139 Calculate the formula(水题,又一个用JavaAC不了的题目)
98 0
|
机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
POJ 1775 (ZOJ 2358) Sum of Factorials
145 0
ZOJ Problem Set - 3758 素数
ZOJ Problem Set - 3758 素数
100 0
|
机器学习/深度学习
|
人工智能 BI 应用服务中间件
|
人工智能 机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions t...
1145 0