SGU 194. Reactor Cooling (无源汇有上下界最大流)

模板题了! SGU194

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

sum(j=1..N, fij) = sum(j=1..N, fji)

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.

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 ≤ 105 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

Output

Test #1 NO Test #2 YES 1 2 3 2 1 1

题目大意:给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质。并且满足每根pipe一定的流量限制,范围为\[Li,Ri\].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li。 建图模型: 对于每根管子有一个上界容量up和一个下界容量low,我们让这根管子的容量下界变为0,上界为up-low。可是这样做了的话流量就不守恒了,为了再次满足流量守恒,即每个节点"入流=出流”,我们增设一个超级源点st和一个超级终点sd。我们开设一个数组du\[\]来记录每个节点的流量情况。 du\[i\]=in\[i\](i节点所有入流下界之和)-out\[i\](i节点所有出流下界之和)。 当du\[i\]大于0的时候,st到i连一条流量为du\[i\]的边。 当du\[i\]小于0的时候,i到sd连一条流量为-du\[i\]的边。 最后对(st,sd)求一次最大流即可,当所有附加边全部满流时(即maxflow==所有du\[\]>0之和),有可行解。   经典的建图了,直接上最大流模板即可。 然后题目要输出一个解。

/* ***
Author :kuangbin
Created Time :2014/12/28 17:20:14
File Name :E:\2014ACM\SGU\SGU194.cpp
************************************************ */

#include <stdio.h>

#include <string.h>

#include

#include

#include

#include

#include

#include

#include

#include <math.h>

#include <stdlib.h>

#include <time.h>
using namespace std;

const int MAXN = 210;//点数的最大值
const int MAXM = 40010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge{
int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
void init(){
tol = 0;
memset(head,-1,sizeof(head));
}
int id[MAXM];
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0){
edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
edge[tol].flow = 0;head[u] = tol++;
edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
edge[tol].flow = 0;head[v]=tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N){
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = N;
int ans = 0;
while(dep[start] < N){
if(u == end){
int Min = INF;
for(int i = pre[u];i != -1; i = pre[edge[i^1].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i = pre[u];i != -1; i = pre[edge[i^1].to]){
edge[i].flow += Min;
edge[i^1].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -1;i = edge[i].next){
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u]){
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag){
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -1;i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min){
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]–;
if(!gap[dep[u]])return ans;
dep[u] = Min+1;
gap[dep[u]]++;
if(u != start) u = edge[pre[u]^1].to;
}
return ans;
}
int up[MAXM],down[MAXN];
int a[MAXN];

int main()
{
//freopen(“in.txt”,”r”,stdin);
//freopen(“out.txt”,”w”,stdout);
int n,m;
while(scanf(“%d%d”,&n,&m) == 2){
init();
memset(a,0,sizeof(a));
int u,v;
for(int i = 0;i < m;i++){
scanf(“%d%d%d%d”,&u,&v,&down[i],&up[i]);
a[u] -= down[i];
a[v] += down[i];
id[i] = tol;
addedge(u,v,up[i]-down[i]);
}
int sum = 0;
for(int i = 1;i <= n;i++){
if(a[i] > 0){
addedge(0,i,a[i]);
sum += a[i];
}
else if(a[i] < 0){
addedge(i,n+1,-a[i]);
}
}
if(sap(0,n+1,n+2) < sum)printf(“NO\n”);
else {
printf(“YES\n”);
for(int i = 0;i < m;i++){
printf(“%d\n”,down[i]+edge[id[i]].flow);
}
}
}
return 0;
}

------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 534.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%