首页 > 代码库 > ZOJ2314 Reactor Cooling
ZOJ2314 Reactor Cooling
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
被迫沉迷文化课,更博速度骤减……
无源汇的上下界网络流
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<queue> 8 using namespace std; 9 const int mxn=320; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 13 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 14 return x*f; 15 } 16 struct edge{ 17 int v,nxt,f; 18 }e[mxn*mxn*2],c[mxn*mxn*2]; 19 int hd[mxn],mct=1; 20 void add_edge(int u,int v,int c){ 21 e[++mct].v=v;e[mct].f=c;e[mct].nxt=hd[u];hd[u]=mct;return; 22 } 23 void insert(int u,int v,int c){ 24 add_edge(u,v,c);add_edge(v,u,0);return; 25 } 26 int n,m,S,T; 27 int in[mxn],low[mxn*mxn*2];//待平衡流量 最小流量 28 int d[mxn]; 29 bool BFS(){ 30 memset(d,0,sizeof d); 31 d[S]=1; 32 queue<int>q; 33 q.push(S); 34 while(!q.empty()){ 35 int u=q.front();q.pop(); 36 for(int i=hd[u];i;i=e[i].nxt){ 37 int v=e[i].v; 38 if(!d[v] && e[i].f){ 39 d[v]=d[u]+1;q.push(v); 40 } 41 } 42 } 43 return d[T]; 44 } 45 int DFS(int u,int lim){ 46 if(u==T)return lim; 47 int tmp,f=0; 48 for(int i=hd[u];i;i=e[i].nxt){ 49 int v=e[i].v; 50 if(d[v]==d[u]+1 && e[i].f){ 51 tmp=DFS(v,min(lim,e[i].f)); 52 e[i].f-=tmp; 53 e[i^1].f+=tmp; 54 lim-=tmp; 55 f+=tmp; 56 if(!lim)return f; 57 } 58 } 59 d[u]=0; 60 return f; 61 } 62 int Dinic(){ 63 int res=0; 64 while(BFS())res+=DFS(S,1e9); 65 return res; 66 } 67 bool pd(){ 68 for(int i=hd[S];i;i=e[i].nxt) 69 if(e[i].f)return 0;//附加边未跑满流,不可行 70 return 1; 71 } 72 void init(){ 73 memset(hd,0,sizeof hd); 74 memset(in,0,sizeof in); 75 mct=1; 76 return; 77 } 78 int main(){ 79 int cas=read(); 80 int i,j,u,v,w; 81 while(cas--){ 82 init(); 83 n=read();m=read(); 84 S=0;T=n+1; 85 for(i=1;i<=m;i++){ 86 u=read();v=read();low[i]=read();w=read(); 87 in[u]-=low[i];in[v]+=low[i]; 88 insert(u,v,w-low[i]); 89 } 90 for(i=1;i<=n;i++){//强制平衡流量 91 if(in[i]>0)insert(S,i,in[i]); 92 else if(in[i]<0)insert(i,T,-in[i]); 93 } 94 Dinic(); 95 if(!pd()){ 96 printf("NO\n\n"); 97 continue; 98 } 99 printf("YES\n");100 for(i=1;i<=m;i++){101 printf("%d\n",e[(i*2)^1].f+low[i]);102 }103 printf("\n");104 }105 return 0;106 }
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
ZOJ2314 Reactor Cooling