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         else
144         {
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 }
时间: 2024-08-25 16:30:09

zoj 2314 Reactor Cooling 网络流的相关文章

zoj 2314 Reactor Cooling (无源汇上下界可行流)

Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear re

ZOJ 2314 Reactor Cooling(无源汇上下界网络流)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题意: 给出每条边流量的上下界,问是否存在可行流,如果存在则输出. 思路:先定义D(u)为顶点u发出的所有弧的流量下界与进入顶点u的所有弧的流量下界和之差(out[u]-in[u]). 对于无源汇的网络流来说: (1)新增两个顶点S(附加源点)和T(附加汇点). (2)对原网络中每个顶点u,计算出D(u),如果D(u)>0,则增加一条新弧<u,T>,这条弧的

ZOJ 2314 Reactor Cooling 带上下界的网络流

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质. 并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li. 求的是最大流. 很久之前就看了带上下界的网络流,一直没看懂

ZOJ 2314 Reactor Cooling 无源汇有上下界网络流

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意:给出N个点,M条边的有向图,每条边的有上下界规定,问是否存在一个可行流满足条件,如果满足输出YES并输出每条边的流量. 如果不满足输出NO. 根据周源的<一种简易的方法求解流量有上下界的网络中网络流问题> 无源汇上下界网络流的做法是: 设边u->v的下界是B(u,v),上界是C(u,v). 设M(i)为对于i结点的流入i的下界总和-流出i的下界总

ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质. 并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li. 解题思路: 转自:https://www.cnbl

zoj 2314 Reactor Cooling 有上下界的网络最大流

输出的时候发现不会对原来的矩阵排序,只好重新搞了一储存边的一维数组,然后排序. #include<bits/stdc++.h> using namespace std; const int N=256; const int inf=0x7fffffff; struct type { int b,c,f; int no; }; struct node { int no,f; }g[20000+5]; int cmp(node a,node b) { return a.no<b.no; }

【有上下界网络流】【ZOJ】2314 Reactor Cooling

[算法]有上下界网络流-无源汇(循环流) [题解] 无源汇网络流相当于重建图后跑最大流. 循环流要求每个点(或边)的流入量和流出量相等. http://www.cnblogs.com/liu-runda/p/6262832.html http://hzwer.com/3356.html 入度>出度时(in[x]>0)时,需要流出去,所以从源向点引一条边,引诱它流出去. 入度<出度时(in[x]<0)时,需要流进来,所以从点向汇引一条边,引诱它流进来. 为何这样正确?源和汇的作用只是

Reactor Cooling ZOJ - 2314 上下界网络流

Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> #include<iostream> #include<cstring> using namespace std; const int maxn=600; const int INF=1000000; # define pb push_back int A[maxn],mapp[INF

SGU 194 Reactor Cooling ——网络流

[题目分析] 无源汇上下界可行流. 上下界网络流的问题可以参考这里.↓ http://www.cnblogs.com/kane0526/archive/2013/04/05/3001108.html [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> //#include <map> #include <set> #include