2个经典 zoj2314 无源汇有上下界最大流 并输出可行流 ZOJ3229 有源汇上下界最大流

来源:互联网 发布:修改软件内部网址 编辑:程序博客网 时间:2024/05/30 13:41
ZOJ Problem Set - 2314
Reactor Cooling

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

The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium for the nuclear bomb they are planning to create. Being the wicked computer genius of this group, you are responsible for developing the cooling system for the reactor.

The cooling system of the reactor consists of the number of pipes that special cooling liquid flows by. Pipes are connected at special points, called nodes, each pipe has the starting node and the end point. The liquid must flow by the pipe from its start point to its end point and not in the opposite direction.

Let the nodes be numbered from 1 to N. The cooling system must be designed so that the liquid is circulating by the pipes and the amount of the liquid coming to each node (in the unit of time) is equal to the amount of liquid leaving the node. That is, if we designate the amount of liquid going by the pipe from i-th node to j-th as fij, (put fij = 0 if there is no pipe from node i to node j), for each i the following condition must hold:

fi,1+fi,2+...+fi,N = f1,i+f2,i+...+fN,i

Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be fij <= cij where cij is the capacity of the pipe. To provide sufficient cooling, the amount of the liquid flowing by the pipe going from i-th to j-th nodes must be at least lij, thus it must be fij >= lij.

Given cij and lij for all pipes, find the amount fij, satisfying the conditions specified above.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

The first line of the input file contains the number N (1 <= N <= 200) - the number of nodes and and M - the number of pipes. The following M lines contain four integer number each - i, j, lij and cij each. There is at most one pipe connecting any two nodes and 0 <= lij <= cij <= 10^5 for all pipes. No pipe connects a node to itself. If there is a pipe from i-th node to j-th, there is no pipe from j-th node to i-th.


Output

On the first line of the output file print YES if there is the way to carry out reactor cooling and NO if there is none. In the first case M integers must follow, k-th number being the amount of liquid flowing by the k-th pipe. Pipes are numbered as they are given in the input file.


Sample Input

2

4 6
1 2 1 2
2 3 1 2
3 4 1 2
4 1 1 2
1 3 1 2
4 2 1 2

4 6
1 2 1 3
2 3 1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3


Sample Input

NO

YES
1
2
3
2
1
1



Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #1
 
 

题意:

   给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质。

并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li。

 

思路:每一个点流进来的流=流出去的流

对于每一个点i,令

Mi= sum(i点所有流进来的下界流)– sum(i点所有流出去的下界流)

如果Mi大于0,代表此点必须还要流出去Mi的自由流,那么我们从源点连一条Mi的边到该点。

如果Mi小于0,代表此点必须还要流进来Mi的自由流,那么我们从该点连一条Mi的边到汇点。

如果求S->T的最大流,看是否满流(S的相邻边都流满)。

满流则有解,否则无解。

 输出可行流  易知道每条边的下标为0+i*2    故其上面的可行流大小为 low[0+i*2]++edge[i^1].cap   具体看代码

 

#include <stdio.h>#include <string.h>#define VM 250#define EM 100000#define inf 0x3f3f3f3fstruct Edge{    int frm,to,cap,next;}edge[EM];int head[VM],dep[VM],ep;     //dep为点的层次void addedge (int cu,int cv,int cw)  //第一条边下标必须为偶数{    edge[ep].frm = cu;    edge[ep].to = cv;    edge[ep].cap = cw;    edge[ep].next = head[cu];    head[cu] = ep;    ep ++;    edge[ep].frm = cv;    edge[ep].to = cu;    edge[ep].cap = 0;    edge[ep].next = head[cv];    head[cv] = ep;    ep ++;}int BFS (int src,int des)     //求出层次图{    int que[VM],i,front = 0,rear = 0;    memset (dep,-1,sizeof(dep));    que[rear++] = src;    dep[src] = 0;    while (front != rear)    {        int u = que[front++];        front = front%VM;        for (i = head[u];i != -1;i = edge[i].next)        {            int v = edge[i].to;            if (edge[i].cap > 0&&dep[v] == -1) //容量大于0&&未在dep中            {                dep[v] = dep[u] + 1;        //建立层次图                que[rear ++] = v;                rear = rear % VM;                if (v == des)  //找到汇点 返回                    return 1;            }        }    }    return 0;}int dinic (int src,int des){    int i,res = 0,top;    int stack[VM];    //stack为栈,存储当前增广路    int cur[VM];        //存储当前点的后继 跟head是一样的    while (BFS(src,des))   //if BFS找到增广路    {        memcpy (cur,head,sizeof (head));        int u = src;       //u为当前结点        top = 0;        while (1)        {            if (u == des)     //增广路已全部进栈            {                int min = inf,loc ;                for (i = 0;i < top;i ++)       //找最小的增广跟并loc记录其在stack中位置                    if (min > edge[stack[i]].cap)  //以便退回该边继续DFS                    {                        min = edge[stack[i]].cap;                        loc = i;                    }                for (i = 0;i < top;i ++)   //偶数^1 相当加1 奇数^1相当减1 当正向边 = 0&&路径不合适时,正加负减                {                           //偶数是正向边,奇数是负向边,边从0开始                    edge[stack[i]].cap -= min;                    edge[stack[i]^1].cap += min;                }                              //将增广路中的所有边修改                res += min;                top = loc;                u = edge[stack[top]].frm;         //当前结点修改为最小边的起点            }            for (i = cur[u];i != -1;cur[u] = i = edge[i].next)   //找到当前结点对应的下一条边                if (edge[i].cap != 0&&dep[u] + 1 == dep[edge[i].to])//不满足条件时,修改cur值(去掉不合适的占)eg:1-->2 1-->3 1-->4 有边 但只有                    break;                                  // 1-->4 这条边满足条件 就把1到2、3的边给去掉            if (cur[u] != -1)            //当前结点的下一条边存在            {                stack[top ++] = cur[u];   //把该边放入栈中                u = edge[cur[u]].to;         //再从下个点开始找            }            else            {                if (top == 0)        //当前结点无未遍历的下一条边且栈空,DFS找不到下一条增广路                    break;                dep[u] = -1;            //当前结点不在增广路中,剔除该点                u = edge[stack[--top]].frm; //退栈 回朔,继续查找            }        }    }    return res;}int in[VM],out[VM],low[EM],up[EM];int main ()///坐标从0或1开始均可  注意别忘记下面的2个初始化{    int m,v1,v2,n,cas,i;    int src,des;    scanf("%d",&cas);    while (cas--)    {        scanf ("%d%d",&n,&m);        ep = 0;//边的初始化        src =0;        des = n+1;        memset (head,-1,sizeof(head));///这里初始化        memset(in,0,sizeof(in));        memset(out,0,sizeof(out));        for(i=0;i<m;i++)        {            scanf("%d %d %d %d",&v1,&v2,&low[i],&up[i]);            addedge (v1,v2,up[i]-low[i]);            in[v2]+=low[i];            out[v1]+=low[i];        }        int sum=0;        for(i=1;i<=n;i++)        {            if(in[i]>out[i])            {                sum+=in[i]-out[i];                addedge(src,i,in[i]-out[i]);            }            else                if(out[i]>in[i])                     addedge(i,des,out[i]-in[i]);        }        int ans = dinic(src,des);        if(sum!=ans)        printf ("NO\n");        else        {            printf("YES\n");            for(i=0;i<2*m;i+=2)            {                printf("%d\n",low[i/2]+edge[i^1].cap);            }        }    }    return 0;}


 

 

ZOJ  3229

 

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 inGensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns theBunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautifuldanmaku(barrange) or cute girls living in Gensokyo. She is the biggest connoisseur of rumors about the girls ofGensokyo among the tengu. Her intelligence gathering abilities are the best inGensokyo!

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 leastGx photos of girl x in total in the Bunkachou. At thek-th day, there are Ck targets, Tk1,Tk2, ..., TkCk. The number of photos of targetTki that Aya takes should be in range [Lki,Rki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use herlast 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. Thenm integers, G1, G2, ..., Gm in range [0, 10000]. Thenn 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 312 12 123 180 3 91 3 92 3 93 180 3 91 3 92 3 92 312 12 123 180 3 91 3 92 3 93 180 0 31 3 62 6 92 312 12 123 150 3 91 3 92 3 93 210 0 31 3 62 6 12

Sample Output

3666666636963369-1
 

题目大意: 一个XX用n天要给m个女神拍写真。这n天里每个女神i分别至少要拍Gi张照片,XX在第j天会给指定Cj个女神最多拍Dj张照片,每个女神第j天拍照数在lj到hj张照片。问XX是否安排完成他的任务,不能输出-1,如果能的话,让他尽可能多拍照。先输出他最多能拍的张数,然后对于输入每一行提到要拍照的女神,按照顺序输出当天那个女神要拍照的张数。(就描述到这了,更多请访问下面的链接)

 

引入源点S汇点T,n天一天一点,m个少女每人一点。S到每一天连一条边,上界为Dt,下界为0。每个少女到汇点连一条边,上界为无穷大,下界为Gi。对每一天,连一条边到所有要拍照的少女,下界为L,上界为R。然后引入超级源点SS,超级汇点TT,像无源无汇上下界可行流那样建边,然后T到S连一条边,容量为无穷大。最后从源点S到汇点T跑一遍最大流就是答案,每条边容量的取法和无源无汇上下界可行流一样。

 

 

 

 

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int MAXN = 2010;const int MAXE = 1000010;const int INF = 0x3fff3fff;struct SAP {    int head[MAXN], gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];    int to[MAXE], next[MAXE], flow[MAXE], cap[MAXE];    int n, ecnt, st, ed;    void init() {        memset(head, 0, sizeof(head));        ecnt = 2;    }    void add_edge(int u, int v, int c) {        to[ecnt] = v; cap[ecnt] = c; flow[ecnt] = 0; next[ecnt] = head[u]; head[u] = ecnt++;        to[ecnt] = u; cap[ecnt] = 0; flow[ecnt] = 0; next[ecnt] = head[v]; head[v] = ecnt++;        //printf("%d->%d %d\n", u, v, c);    }    void bfs() {        memset(dis, 0x3f, sizeof(dis));        queue<int> que; que.push(ed);        dis[ed] = 0;        while(!que.empty()) {            int u = que.front(); que.pop();            ++gap[dis[u]];            for(int p = head[u]; p; p = next[p]) {                int &v = to[p];                if(cap[p ^ 1] > flow[p ^ 1] && dis[v] > n) {                    dis[v] = dis[u] + 1;                    que.push(v);                }            }        }    }    int Max_flow(int ss, int tt, int nn) {        st = ss, ed = tt, n = nn;        int ans = 0, minFlow = INF, u;        for(int i = 0; i <= n; ++i) {            cur[i] = head[i];            gap[i] = 0;        }        u = pre[st] = st;        bfs();        while(dis[st] < n) {            bool flag = false;            for(int &p = cur[u]; p; p = next[p]) {                int &v = to[p];                if(cap[p] > flow[p] && dis[u] == dis[v] + 1) {                    flag = true;                    minFlow = min(minFlow, cap[p] - flow[p]);                    pre[v] = u;                    u = v;                    if(u == ed) {                        ans += minFlow;                        while(u != st) {                            u = pre[u];                            flow[cur[u]] += minFlow;                            flow[cur[u] ^ 1] -= minFlow;                        }                        minFlow = INF;                    }                    break;                }            }            if(flag) continue;            int minDis = n - 1;            for(int p = head[u]; p; p = next[p]) {                int &v = to[p];                if(cap[p] > flow[p] && dis[v] < minDis) {                    minDis = dis[v];                    cur[u] = p;                }            }            if(--gap[dis[u]] == 0) break;            ++gap[dis[u] = minDis + 1];            u = pre[u];        }        return ans;    }} G;int n, m, c, d, l, r, x;int f[MAXN], down[MAXE], id[MAXE];int main() {    while(scanf("%d%d", &n, &m) != EOF) {        G.init();        memset(f, 0, sizeof(f));        int s = n + m + 1, t = s + 1;        int ss = t + 1, tt = ss + 1;        for(int i = 1; i <= m; ++i) {            scanf("%d", &x);            f[i] -= x;            f[t] += x;            G.add_edge(i, t, INF);        }        int cnt = 0;        for(int i = 1; i <= n; ++i) {            scanf("%d%d", &c, &d);            G.add_edge(s, i + m, d);            f[s]-=0;            f[i+m]+=0;            while(c--) {                scanf("%d%d%d", &x, &l, &r);                f[i + m] -= l;                f[x + 1] += l;                down[++cnt] = l; id[cnt] = G.ecnt;                G.add_edge(i + m, x + 1, r - l);            }        }        int sum = 0;        for(int i = 1; i <= t; ++i) {            if(f[i] > 0) sum += f[i], G.add_edge(ss, i, f[i]);            else G.add_edge(i, tt, -f[i]);        }        G.add_edge(t, s, INF);        if(sum != G.Max_flow(ss, tt, tt)) puts("-1");        else {            printf("%d\n", G.Max_flow(s, t, tt));            for(int i = 1; i <= cnt; ++i) printf("%d\n", down[i] + G.flow[id[i]]);        }        puts("");    }}

 
 
参考  :http://www.cnblogs.com/oyking/archive/2013/08/12/3252791.html