Reactor Cooling
Time Limit: 500MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
194. Reactor Cooling
time limit per test: 0.5 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard
output: standard
output: standard
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 f ij, (put f ij = 0 if there is no pipe from node i to node j), for each i the following condition must hold:
sum(j=1..N, f ij) = sum(j=1..N, f ji)
Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be f ij ≤ c ij where c ij 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 l ij, thus it must be f ij ≥ l ij.
Given c ij and l ij for all pipes, find the amount f ij, satisfying the conditions specified above.
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 f ij, (put f ij = 0 if there is no pipe from node i to node j), for each i the following condition must hold:
sum(j=1..N, f ij) = sum(j=1..N, f ji)
Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be f ij ≤ c ij where c ij 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 l ij, thus it must be f ij ≥ l ij.
Given c ij and l ij for all pipes, find the amount f ij, satisfying the conditions specified above.
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, l ij and c ij each. There is at most one pipe connecting any two nodes and 0 ≤ l ij ≤ c ij ≤ 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 test(s)
Input
Test #1
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
Test #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
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
Test #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
Output
Test #1
NO
Test #2
YES
1
2
3
2
1
1
NO
Test #2
YES
1
2
3
2
1
1
题目大意:给n个点,及m根管道,每根管道用来流淌液体的,单向的,每时每刻每根管道流进来的物质要等于流出去的物质,要使得m条管道组成一个循环体,里面流躺物质。并且满足每根管道一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li。
题解:有上下界的网络流 http://blog.sina.com.cn/s/blog_76f6777d0101bara.html
题目来源:SGU 194 这个是单组数据,但是由于是俄国网站,评测超慢,我等了一个多小时还没测。zoj2314是多组数据,只需要每次清空数组就可以了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 using namespace std; 10 const int inf=1e9; 11 const int maxn=500,maxm=300000; 12 int N,M,S,T,maxflow; 13 struct Edge1{ 14 int l,r; 15 }E[maxm]; 16 17 vector<int> ru[maxn],chu[maxn],to1[maxn]; 18 int in[maxn],out[maxn],delta[maxn],ans[maxn]; 19 20 struct Edge2{ 21 int to,rest,next; 22 int num; 23 }e[maxm]; 24 int head[maxn],cnt=1; 25 inline void addedge(int x,int y,int z,int num){ 26 e[++cnt].to=y; e[cnt].rest=z; e[cnt].next=head[x]; head[x]=cnt; e[cnt].num=num; 27 e[++cnt].to=x; e[cnt].rest=0; e[cnt].next=head[y]; head[y]=cnt; e[cnt].num=num; 28 } 29 int dis[maxn]; 30 bool BFS(){ 31 memset(dis,0,sizeof(dis)); 32 static queue<int> Q; 33 while(!Q.empty()) Q.pop(); 34 Q.push(S); dis[S]=1; 35 while(!Q.empty()){ 36 int x=Q.front(); Q.pop(); 37 for(int i=head[x];i;i=e[i].next){ 38 int y=e[i].to; 39 if(e[i].rest&&dis[y]==0){ 40 dis[y]=dis[x]+1; 41 Q.push(y); 42 } 43 } 44 } 45 if(dis[T]>0) return true; 46 return false; 47 } 48 int DFS(int x,int flow){ 49 if(x==T) return flow; 50 int now=0,tmp; 51 for(int i=head[x];i;i=e[i].next){ 52 int y=e[i].to; 53 if(e[i].rest&&dis[y]==dis[x]+1){ 54 tmp=DFS(y,min(flow-now,e[i].rest)); 55 e[i].rest-=tmp; 56 e[i^1].rest+=tmp; 57 now+=tmp; 58 if(now==flow) return now; 59 } 60 } 61 if(!now) dis[x]=0; 62 return now; 63 } 64 void dinic(){ 65 while(BFS()) 66 maxflow+=DFS(S,inf); 67 } 68 int main(){ 69 scanf("%d%d",&N,&M); 70 S=0; T=N+1; 71 for(int i=1,u,v,l,r;i<=M;i++){ 72 scanf("%d%d%d%d",&u,&v,&l,&r); 73 ru[v].push_back(i); chu[u].push_back(i); to1[u].push_back(v); 74 E[i].l=l; E[i].r=r; 75 } 76 for(int i=1;i<=N;i++){ 77 for(int j=0;j<ru[i].size();j++){ 78 int num=ru[i][j]; 79 in[i]+=E[num].l; 80 } 81 for(int j=0;j<chu[i].size();j++){ 82 int num=chu[i][j]; 83 out[i]+=E[num].l; 84 } 85 } 86 for(int i=1;i<=N;i++) delta[i]=in[i]-out[i]; 87 88 for(int i=1;i<=N;i++){ 89 for(int j=0;j<chu[i].size();j++){ 90 int v=to1[i][j],num=chu[i][j]; 91 addedge(i,v,E[num].r-E[num].l,num); 92 } 93 } 94 for(int i=1;i<=N;i++){ 95 if(delta[i]>0) addedge(S,i,delta[i],0); 96 if(delta[i]<0) addedge(i,T,-delta[i],0); 97 } 98 99 dinic(); 100 for(int i=head[S];i;i=e[i].next){ 101 if(e[i].rest>0){ 102 puts("NO"); 103 return 0; 104 } 105 } 106 puts("YES"); 107 for(int i=2;i<M*2+1;i+=2) ans[e[i^1].num]=e[i^1].rest; 108 for(int i=1;i<=M;i++) printf("%d\n",ans[i]+E[i].l); 109 return 0; 110 }
zoj多组数据的如下
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 using namespace std; 10 const int inf=1e9; 11 const int maxn=5000,maxm=1000000; 12 int tot,N,M,S,T,maxflow; 13 struct Edge1{ 14 int l,r; 15 }E[maxm]; 16 17 vector<int> ru[maxn],chu[maxn],to1[maxn]; 18 int in[maxn],out[maxn],delta[maxn],ans[maxn]; 19 20 struct Edge2{ 21 int to,rest,next; 22 int num; 23 }e[maxm]; 24 int head[maxn],cnt=1; 25 inline void addedge(int x,int y,int z,int num){ 26 e[++cnt].to=y; e[cnt].rest=z; e[cnt].next=head[x]; head[x]=cnt; e[cnt].num=num; 27 e[++cnt].to=x; e[cnt].rest=0; e[cnt].next=head[y]; head[y]=cnt; e[cnt].num=num; 28 } 29 int dis[maxn]; 30 bool BFS(){ 31 memset(dis,0,sizeof(dis)); 32 static queue<int> Q; 33 while(!Q.empty()) Q.pop(); 34 Q.push(S); dis[S]=1; 35 while(!Q.empty()){ 36 int x=Q.front(); Q.pop(); 37 for(int i=head[x];i;i=e[i].next){ 38 int y=e[i].to; 39 if(e[i].rest&&dis[y]==0){ 40 dis[y]=dis[x]+1; 41 Q.push(y); 42 } 43 } 44 } 45 if(dis[T]>0) return true; 46 return false; 47 } 48 int DFS(int x,int flow){ 49 if(x==T) return flow; 50 int now=0,tmp; 51 for(int i=head[x];i;i=e[i].next){ 52 int y=e[i].to; 53 if(e[i].rest&&dis[y]==dis[x]+1){ 54 tmp=DFS(y,min(flow-now,e[i].rest)); 55 e[i].rest-=tmp; 56 e[i^1].rest+=tmp; 57 now+=tmp; 58 if(now==flow) return now; 59 } 60 } 61 if(!now) dis[x]=0; 62 return now; 63 } 64 void dinic(){ 65 while(BFS()) 66 maxflow+=DFS(S,inf); 67 } 68 int main(){ 69 scanf("%d",&tot); 70 while(tot--){ 71 maxflow=0; 72 memset(ru,0,sizeof(ru)); memset(chu,0,sizeof(chu)); memset(to1,0,sizeof(to1)); 73 memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); 74 memset(delta,0,sizeof(delta)); memset(ans,0,sizeof(ans)); 75 memset(head,0,sizeof(head)); 76 for(int i=1;i<=cnt;i++) { 77 e[i].next=e[i].num=e[i].rest=e[i].rest=e[i].to=0; 78 } 79 cnt=1; 80 81 scanf("%d%d",&N,&M); 82 S=0; T=N+1; 83 for(int i=1,u,v,l,r;i<=M;i++){ 84 scanf("%d%d%d%d",&u,&v,&l,&r); 85 ru[v].push_back(i); chu[u].push_back(i); to1[u].push_back(v); 86 E[i].l=l; E[i].r=r; 87 } 88 for(int i=1;i<=N;i++){ 89 for(int j=0;j<ru[i].size();j++){ 90 int num=ru[i][j]; 91 in[i]+=E[num].l; 92 } 93 for(int j=0;j<chu[i].size();j++){ 94 int num=chu[i][j]; 95 out[i]+=E[num].l; 96 } 97 } 98 for(int i=1;i<=N;i++) delta[i]=in[i]-out[i]; 99 100 for(int i=1;i<=N;i++){ 101 for(int j=0;j<chu[i].size();j++){ 102 int v=to1[i][j],num=chu[i][j]; 103 addedge(i,v,E[num].r-E[num].l,num); 104 } 105 } 106 for(int i=1;i<=N;i++){ 107 if(delta[i]>0) addedge(S,i,delta[i],0); 108 if(delta[i]<0) addedge(i,T,-delta[i],0); 109 } 110 111 dinic(); 112 for(int i=head[S];i;i=e[i].next){ 113 if(e[i].rest>0){ 114 puts("NO"); 115 goto end1; 116 } 117 } 118 puts("YES"); 119 for(int i=2;i<M*2+1;i+=2) ans[e[i^1].num]=e[i^1].rest; 120 for(int i=1;i<=M;i++) printf("%d\n",ans[i]+E[i].l); 121 end1:; 122 } 123 return 0; 124 }
: