首页 > 代码库 > zoj 2314 Reactor Cooling 网络流

zoj 2314 Reactor Cooling 网络流

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314

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.

题意:很裸的题意,仔细读一下就明白了,就是一个无源汇的上下界网络流。

解法:无源汇的上下界网络流。下面简单介绍一下无源汇的上下界网络流构图方法,如有不当或不足之处,感谢提出。

针对边的构造  :  

增加新源点from和汇点to,对于原图中任意一条边u->v,上下界流量分别为b,c,构造新图时,u->v的上界为c-b,新源点from->v上界为b,u->新汇点to上界为b。这样,就去掉了流量下界的控制。

参考书籍:《挑战程序设计竞赛》

针对点的构造  :  

增加新源点from和汇点to,原图中的边u->v的上界为c-b(和上面一样),对于原图中任意一个点u,统计所有流入u点的流量下界和 in 和经过u点流出的所有流量下界和out ,如果 in > out ,那么就连一条边from->u上界为in-out,否则就连一条边u->to上界为out-in。

参考论文:《一种简易的方法求解流量有上下界的网络中网络流问题》

  1 #include<iostream>  2 #include<cstdio>  3 #include<cstring>  4 #include<cstdlib>  5 #include<cmath>  6 #include<algorithm>  7 #include<queue>  8 #define inf 0x7fffffff  9 using namespace std; 10 const int maxn=200+10; 11 const int M = 40000+100; 12  13 struct Edge 14 { 15     int to,cap,next; 16     int w; 17     Edge (){} 18     Edge (int to2,int cap2,int next2,int w2){to=to2,cap=cap2,next=next2,w=w2;} 19 }edge[M*3]; 20 int head[maxn],edgenum; 21 int n,m,from,to,vnum; 22 int id[M],c[M]; 23  24 void add(int u,int v,int cap) 25 { 26     edge[edgenum].to=v; 27     edge[edgenum].cap=cap; 28     edge[edgenum].w=cap; 29     edge[edgenum].next=head[u]; 30     head[u]=edgenum ++ ; 31  32     edge[edgenum].to=u; 33     edge[edgenum].cap=0; 34     edge[edgenum].w=cap; 35     edge[edgenum].next=head[v]; 36     head[v]=edgenum ++ ; 37 } 38  39 int level[maxn]; 40 int gap[maxn]; 41 void bfs(int to) 42 { 43     memset(level,-1,sizeof(level)); 44     memset(gap,0,sizeof(gap)); 45     level[to]=0; 46     gap[level[to] ]++; 47     queue<int> Q; 48     Q.push(to); 49     while (!Q.empty()) 50     { 51         int u=Q.front() ;Q.pop() ; 52         for (int i=head[u] ;i!=-1 ;i=edge[i].next) 53         { 54             int v=edge[i].to; 55             if (level[v] != -1) continue; 56             level[v]=level[u]+1; 57             gap[level[v] ]++; 58             Q.push(v); 59         } 60     } 61 } 62  63 int pre[maxn]; 64 int cur[maxn]; 65 int SAP(int from,int to) 66 { 67     bfs(to); 68     memset(pre,-1,sizeof(pre)); 69     memcpy(cur,head,sizeof(head)); 70     int u=pre[from]=from,flow=0,aug=inf; 71     gap[0]=vnum; 72     while (level[from]<vnum) 73     { 74         bool flag=false; 75         for (int &i=cur[u] ;i!=-1 ;i=edge[i].next) 76         { 77             int v=edge[i].to; 78             if (edge[i].cap>0 && level[u]==level[v]+1) 79             { 80                 flag=true; 81                 aug=min(aug,edge[i].cap); 82                 pre[v]=u; 83                 u=v; 84                 if (v==to) 85                 { 86                     flow += aug; 87                     for (u=pre[v] ;v!=from ;v=u,u=pre[u]) 88                     { 89                         edge[cur[u] ].cap -= aug; 90                         edge[cur[u]^1 ].cap += aug; 91                     } 92                     aug=inf; 93                 } 94                 break; 95             } 96         } 97         if (flag) continue; 98         int minlevel=vnum; 99         for (int i=head[u] ;i!=-1 ;i=edge[i].next)100         {101             int v=edge[i].to;102             if (edge[i].cap>0 && level[v]<minlevel)103             {104                 minlevel=level[v];105                 cur[u]=i;106             }107         }108         if (--gap[level[u] ]==0) break;109         level[u]=minlevel+1;110         gap[level[u] ]++;111         u=pre[u];112     }113     return flow;114 }115 116 int main()117 {118     int t;119     scanf("%d",&t);120     while (t--)121     {122         int u,v,w;123         scanf("%d%d",&n,&m);124         memset(head,-1,sizeof(head));125         edgenum=0;126         from=0 ;to=n+1 ;127         vnum=n+2;128         int sum=0;129         for (int i=1 ;i<=m ;i++)130         {131             scanf("%d%d%d%d",&u,&v,&c[i],&w);132             sum += c[i];133             id[i]=edgenum;134             add(u,v,w-c[i]);135             add(from,v,c[i]);136             add(u,to,c[i]);137         }138         int Maxflow=SAP(from,to);139         int flag=0;140         for (int i=head[from] ;i!=-1 ;i=edge[i].next)141             if (edge[i].cap) {flag=1;break; }142         if (flag) printf("NO\n");143         else144         {145             printf("YES\n");146             for (int i=1 ;i<=m ;i++)147                 printf("%d\n",edge[id[i]^1 ].cap+c[i]);148                 //printf("%d\n",edge[id[i] ].w-edge[id[i] ].cap+c[i]);149         }150         if (t) printf("\n");151     }152     return 0;153 }

 

zoj 2314 Reactor Cooling 网络流