zoj2314 经典 无源汇有上下界最大流 并输出可行流
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:
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 0x3f3f3f3f struct 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; }