题目链接: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。
参考论文:《一种简易的方法求解流量有上下界的网络中网络流问题》
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; struct Edge
{
int to,cap,next;
int w;
Edge (){}
Edge (int to2,int cap2,int next2,int w2){to=to2,cap=cap2,next=next2,w=w2;}
}edge[M*];
int head[maxn],edgenum;
int n,m,from,to,vnum;
int id[M],c[M]; void add(int u,int v,int cap)
{
edge[edgenum].to=v;
edge[edgenum].cap=cap;
edge[edgenum].w=cap;
edge[edgenum].next=head[u];
head[u]=edgenum ++ ; edge[edgenum].to=u;
edge[edgenum].cap=;
edge[edgenum].w=cap;
edge[edgenum].next=head[v];
head[v]=edgenum ++ ;
} int level[maxn];
int gap[maxn];
void bfs(int to)
{
memset(level,-,sizeof(level));
memset(gap,,sizeof(gap));
level[to]=;
gap[level[to] ]++;
queue<int> Q;
Q.push(to);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].to;
if (level[v] != -) continue;
level[v]=level[u]+;
gap[level[v] ]++;
Q.push(v);
}
}
} int pre[maxn];
int cur[maxn];
int SAP(int from,int to)
{
bfs(to);
memset(pre,-,sizeof(pre));
memcpy(cur,head,sizeof(head));
int u=pre[from]=from,flow=,aug=inf;
gap[]=vnum;
while (level[from]<vnum)
{
bool flag=false;
for (int &i=cur[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].to;
if (edge[i].cap> && level[u]==level[v]+)
{
flag=true;
aug=min(aug,edge[i].cap);
pre[v]=u;
u=v;
if (v==to)
{
flow += aug;
for (u=pre[v] ;v!=from ;v=u,u=pre[u])
{
edge[cur[u] ].cap -= aug;
edge[cur[u]^ ].cap += aug;
}
aug=inf;
}
break;
}
}
if (flag) continue;
int minlevel=vnum;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].to;
if (edge[i].cap> && level[v]<minlevel)
{
minlevel=level[v];
cur[u]=i;
}
}
if (--gap[level[u] ]==) break;
level[u]=minlevel+;
gap[level[u] ]++;
u=pre[u];
}
return flow;
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int u,v,w;
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
edgenum=;
from= ;to=n+ ;
vnum=n+;
int sum=;
for (int i= ;i<=m ;i++)
{
scanf("%d%d%d%d",&u,&v,&c[i],&w);
sum += c[i];
id[i]=edgenum;
add(u,v,w-c[i]);
add(from,v,c[i]);
add(u,to,c[i]);
}
int Maxflow=SAP(from,to);
int flag=;
for (int i=head[from] ;i!=- ;i=edge[i].next)
if (edge[i].cap) {flag=;break; }
if (flag) printf("NO\n");
else
{
printf("YES\n");
for (int i= ;i<=m ;i++)
printf("%d\n",edge[id[i]^ ].cap+c[i]);
//printf("%d\n",edge[id[i] ].w-edge[id[i] ].cap+c[i]);
}
if (t) printf("\n");
}
return ;
}