ZOJ2314 Reactor Cooling

Reactor Cooling


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 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.

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 }

Reactor Cooling


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 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.

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

时间: 2024-08-10 02:11:11

ZOJ2314 Reactor Cooling的相关文章

zoj2314 Reactor Cooling --- 上下界可行流

题目给出了每条边的上下界, 此类题目的建边方法是: 1.添加源点汇点, 2.对每条边 添加边 c(u,v) = up(u,v) - low(u,v) 3.对每个点 c(s,v) = out(v) c(v,t) = in(v)   (权值为正) 求s到t的最大流,若最大流等于所有边下界的和,则存在可行流, 每条边的流量为 flow(u,v) +low(u,v) #include <iostream> #include <cstring> #include <string>

ZOJ2314 Reactor Cooling(无源汇流量有上下界网络的可行流)

题目大概说一个核反应堆的冷却系统有n个结点,有m条单向的管子连接它们,管子内流量有上下界的要求,问能否使液体在整个系统中循环流动. 本质上就是求一个无源汇流量有上下界的容量网络的可行流,因为无源汇的容量网络上各个顶点都满足流量平衡条件,即所有点的∑流入流量=∑流出流量,可以看成里面的流是循环流动的,类似有向图欧拉回路. 而带上下界的网络可行流的求法,是根据网络流中一个流是可行流的充分必要条件——限制条件和平衡条件,去改造原网络,转化成不带下界的容量网络来求解的.数学模型那些证明之类的不难理解,见

ZOJ2314 Reactor Cooling 无源汇有上下界最大流

推荐看这里 #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; int n, m, uu, vv, ww, cc, hea[225], cnt, ss, tt, maxFlow, lev[225], tot, T; const int oo=0x3f3f3f3f; struct Edge{ int too, nxt, ca

acdream 1211 Reactor Cooling 【边界网络流量 + 输出流量】

称号:acdream 1211 Reactor Cooling 分类:无汇的有上下界网络流. 题意: 给n个点.及m根pipe,每根pipe用来流躺液体的.单向的.每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体.里面流躺物质. 而且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题).同一时候最小不能低于Li. 比如: 46(4个点,6个pipe) 12 1 3 (1->2上界为3,下界为1) 23 1 3

acdream 1211 Reactor Cooling 【上下界网络流 + 输出流量】

题目:acdream 1211 Reactor Cooling 分类:无源无汇的有上下界网络流. 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质. 并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li. 例如: 46(4个点,6个pipe) 12 1 3 (1->2上界为3,下界为1) 23 1 3

zoj Reactor Cooling

Reactor Cooling 无源汇上下界最大流问题. 1.流量平衡. 2.满足上下界 模板题. #include <iostream> #include <queue> #include <vector> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 200000 + 10; const int INF = 1 << 30; s

ZOJ 2314/SGU194 Reactor Cooling

无源汇点上下界可行流问题..... 建图: 对于一条边 u--->v  low(u,v) high(u,v) 连边 u--->v high(u,v) - low(u,v)  就变成了无上下界的网络流问题了 但这样不一定满足low的关系 ,所以我每要再每个点流量后面加上low..... 设自由流g(u,v)=high(u,v) - low(u,v) 每一个点的流量由自由流g和下界流low组成.... 变一下型: 可以看到每个点流入流出的流量不一定平衡... 我们用一个数组low记录每个点的下界流

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

【SGU 194】 Reactor Cooling

194. Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output: standard The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium for t