Description
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
Solution
题意是说有n个节点m根管道,每个管道有方向且有流量的上界与下界,问是否有可行流,若可行则输出每根管道的流量。
有上下界的网络流·裸
本来是在做ZOJ 3229的,做着做着发现自己并没有领会有上下界网络流的奥义,于是return到这里。。
在无源无汇的情况下,我们人为制造一个源和汇
记录一个in数组,就是流入该点的下界减去流出的下界
如果in[i]>0 则连一条源到这点 容量为 in[i] 的边
如果in[i]<0 则连一条这点到汇 容量为|in[i]|的边
于是在加边的时候,cap应该为上界-下界(暂且称作自由流),就是假装下界已经流满了
然后做一次网络流
若源连出的所有边都已满流,则说明可行
以上内容的证明详见论文周源:一种简易的方法求解流量有上下界的网络中网络流问题
然后要求每根管子的流量,就是下界+自由流,而自由流应该是Edges[((i-1)*2)^1].cap
之所以是(i-1)*2是因为边是从cnt=0开始计数的,异或是它的反向边,反向边cap的增加即该边cap的减少,就是流过的流量
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<string>
#define Min(a,b) (a<b?a:b)
#define INF 0x3f3f3f3f
using namespace std;
int T;
int n,m,s,t;
int head[250],cnt=0,in[250],level[250],low[40005];
struct Node{
int next,to,cap;
}Edges[80005];
void addedge(int u,int v,int c)
{
Edges[cnt].cap=c;
Edges[cnt].to=v;
Edges[cnt].next=head[u];
head[u]=cnt;
cnt++;
}
bool bfs()
{
memset(level,-1,sizeof(level));
queue<int>q;
level[s]=0;
q.push(s);
while(!q.empty())
{
int u=q.front();
for(int i=head[u];~i;i=Edges[i].next)
{
int v=Edges[i].to;
if(level[v]==-1&&Edges[i].cap)
{
level[v]=level[u]+1;
q.push(v);
}
}
q.pop();
}
if(level[t]==-1)return false;
return true;
}
int dfs(int u,int flow)
{
if(u==t)return flow;
int Maxu=0,d=0;
for(int i=head[u];~i&&Maxu<flow;i=Edges[i].next)
{
int v=Edges[i].to;
if(level[v]==level[u]+1&&Edges[i].cap)
{
d=dfs(v,Min(flow-Maxu,Edges[i].cap));
Edges[i].cap-=d;
Edges[i^1].cap+=d;
Maxu+=d;
}
}
if(!Maxu)level[u]=-1;
return Maxu;
}
bool judge()
{
while(bfs())
{
while(dfs(s,INF));
}
for(int i=head[s];~i;i=Edges[i].next)
{
if(Edges[i].cap)
{
return false;
}
}
return true;
}
int main()
{
scanf("%d",&T);
while(T--&&~scanf("%d%d",&n,&m))
{
cnt=0;
memset(low,0,sizeof(low));
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
for(int i=1;i<=m;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
addedge(a,b,d-c);
addedge(b,a,0);
low[i]=c;
in[a]-=c;
in[b]+=c;
}
s=0;t=n+1;
for(int i=1;i<=n;i++)
{
if(in[i]<0)
{
addedge(i,t,-in[i]);
addedge(t,i,0);
}
else if(in[i]>0)
{
addedge(s,i,in[i]);
addedge(i,s,0);
}
}
if(judge())
{
printf("YES\n");
for(int i=1;i<=m;i++)
{
printf("%d\n",Edges[((i-1)*2)^1].cap+low[i]);
}
}
else printf("NO\n");
}
return 0;
}