SGU 194 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 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:
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.
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
一个网络没有源点汇点,里面留着液体,每条边除了流量有上限之外还有下限,问是否能满足所有边的流量约束,能的话输出每条边的流量
如果没有下界我们是会做的,其实下界就是0,我们建边的时候u到v建一条容量r-l的边,这样下界就是0了
此时我们需要平衡流量,我们虚拟一个源点与汇点,对于每条边的u,v.从源点连一条容量为l的边到v,从汇点连一条容量为l的边到u
这样我们就平衡了流量.然后跑最大流.
如果最大流等于与源点相连的边的流量(其实就是每个边的下界之和)就可行
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const int maxn = 2000010; 5 const int inf = 0x3f3f3f3f; 6 struct Edge 7 { 8 int from,to,cap,flow; 9 Edge (int f,int t,int c,int fl) 10 { 11 from=f,to=t,cap=c,flow=fl; 12 } 13 }; 14 struct Dinic 15 { 16 int n,m,s,t; 17 vector <Edge> edge; 18 vector <int> G[maxn];//存图 19 bool vis[maxn];//标记每点是否vis过 20 int cur[maxn];//当前弧优化 21 int dep[maxn];//标记深度 22 void init(int n,int s,int t)//初始化 23 { 24 this->n=n;this->s=s;this->t=t; 25 edge.clear(); 26 for (int i=0;i<n;++i) G[i].clear(); 27 } 28 void addedge (int from,int to,int cap)//加边,单向边 29 { 30 edge.push_back(Edge(from,to,cap,0)); 31 edge.push_back(Edge(to,from,0,0)); 32 m=edge.size(); 33 G[from].push_back(m-2); 34 G[to].push_back(m-1); 35 } 36 bool bfs () 37 { 38 queue<int> q; 39 while (!q.empty()) q.pop(); 40 memset(vis,false,sizeof vis); 41 vis[s]=true; 42 dep[s]=0; 43 q.push(s); 44 while (!q.empty()){ 45 int u=q.front(); 46 //printf("%d\n",u); 47 q.pop(); 48 for (int i=0;i<G[u].size();++i){ 49 Edge e=edge[G[u][i]]; 50 int v=e.to; 51 if (!vis[v]&&e.cap>e.flow){ 52 vis[v]=true; 53 dep[v]=dep[u]+1; 54 q.push(v); 55 } 56 } 57 } 58 return vis[t]; 59 } 60 int dfs (int x,int mi) 61 { 62 if (x==t||mi==0) return mi; 63 int flow=0,f; 64 for (int &i=cur[x];i<G[x].size();++i){ 65 Edge &e=edge[G[x][i]]; 66 int y=e.to; 67 if (dep[y]==dep[x]+1&&(f=dfs(y,min(mi,e.cap-e.flow)))>0){ 68 e.flow+=f; 69 edge[G[x][i]^1].flow-=f; 70 flow+=f; 71 mi-=f; 72 if (mi==0) break; 73 } 74 } 75 return flow; 76 } 77 int max_flow () 78 { 79 int ans = 0; 80 while (bfs()){ 81 memset(cur,0,sizeof cur); 82 ans+=dfs(s,inf); 83 } 84 return ans; 85 } 86 }dinic; 87 int full_flow; 88 int n,m; 89 int id[maxn]; 90 int low[maxn]; 91 int main() 92 { 93 //freopen("de.txt","r",stdin); 94 while (~scanf("%d%d",&n,&m)){ 95 full_flow = 0; 96 int src = 0,dst = n+1; 97 dinic.init(maxn,src,dst); 98 for (int i=1;i<=m;++i){ 99 int u,v,l,r; 100 scanf("%d%d%d%d",&u,&v,&l,&r); 101 //printf("%d %d %d %d\n",u,v,l,r); 102 full_flow+=l; 103 low[i]=l; 104 dinic.addedge(u,v,r-l); 105 id[i] = dinic.edge.size()-2; 106 //printf("%d %d %d\n",dinic.edge[id[i]].from,dinic.edge[id[i]].to,dinic.edge[id[i]].cap); 107 dinic.addedge(src,v,l); 108 dinic.addedge(u,dst,l); 109 } 110 if (dinic.max_flow()!=full_flow){ 111 printf("NO\n"); 112 } 113 else{ 114 printf("YES\n"); 115 for (int i=1;i<=m;++i){ 116 printf("%d\n",low[i]+dinic.edge[id[i]].flow); 117 } 118 } 119 } 120 return 0; 121 }