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:
f i,1+f i,2+...+f i,N = f 1,i+f 2,i+...+f N,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
题意:
给n个点,m根单向管子,用管子来流躺物质,每时每刻每根管子流进来的物质量要等于流出去的物质量。要使得m条管子组成一个循环体,里面流躺物质,并且满足每根管子流量限制范围为[Li,Ri]。即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li。
题解:
上界用ci表示,下界用di表示。下界是必须流满的,那么对于每一条边,去掉下界后,其自由流为ci– di。
主要思想:每一个点流进来的流 = 流出去的流
对于每一个点i,令Mi = sum(i点所有流进来的下界流)– sum(i点所有流出去的下界流)
如果Mi大于0,代表此点必须还要流出去Mi的自由流,那么我们从源点S连一条Mi的边到该点。
如果Mi小于0,代表此点必须还要流进来Mi的自由流,那么我们从该点连一条Mi的边到汇点T。
然后求S->T的最大流,看是否满流。满流则有解,否则无解。
PS:注意邻接表建图的实现方式强力推荐用数组模拟链表法,因为这一题要求输出每条边具体流多少流量,相比较于vector实现的邻接表来说,模拟链表的方式更方便。
代码:
#include
#include
#include
using namespace std;
const int MAXN = 210;
const int INF = 0x3f3f3f3f;
struct Edge{
int flow,to,next;
}E[MAXN*MAXN*2];
int head[MAXN],tot;
inline void Add(int from,int to,int flow){
E[++tot].next = head[from];
E[tot].to = to;
E[tot].flow = flow;
head[from] = tot;
E[++tot].next = head[to];
E[tot].to = from;
E[tot].flow = 0;
head[to] = tot;
}
int deep[MAXN];
bool BFS(int from,int to){
memset(deep,-1,sizeof deep);
deep[from] = 0;
queue Q;
Q.push(from);
while(!Q.empty()){
int t = Q.front();
Q.pop();
for(int i=head[t] ; i ; i=E[i].next){
if(E[i].flow > 0 && deep[E[i].to] == -1){
deep[E[i].to] = deep[t] + 1;
Q.push(E[i].to);
}
}
}
return deep[to] != -1;
}
int DFS(int from,int to,int flow){
if(from == to || flow == 0)return flow;
for(int i=head[from] ; i ; i=E[i].next){
if(E[i].flow > 0 && deep[E[i].to] == deep[from]+1){
int nowflow = DFS(E[i].to,to,min(flow,E[i].flow));
if(nowflow > 0){
E[i].flow -= nowflow;
E[i^1].flow += nowflow;
return nowflow;
}
}
}
return 0;
}
int Dinic(int from,int to){
int sumflow = 0;
while(BFS(from,to)){
int t;
while((t=DFS(from,to,INF)) > 0)sumflow += t;
}
return sumflow;
}
int In[MAXN],Out[MAXN],board[MAXN*MAXN];
inline void init(){
memset(In,0,sizeof In);
memset(Out,0,sizeof Out);
memset(head,0,sizeof head);
tot = 1;
}
int main(){
int T,N,M;
scanf("%d",&T);
while(T--){
init();
scanf("%d %d",&N,&M);
int a,b,c,d;
for(int i=1 ; i<=M ; ++i){
scanf("%d %d %d %d",&a,&b,&c,&d);
In[b] += c;
Out[a] += c;
board[i] = c;
Add(a,b,d-c);
}
int sum = 0;
for(int i=1 ; i<=N ; ++i){
int t = In[i] - Out[i];
if(t > 0){
Add(N+1,i,t);
sum += t;
}
else Add(i,N+2,-t);
}
if(Dinic(N+1,N+2) == sum){
printf("YES\n");
for(int i=1 ; i<=M ; ++i){
printf("%d\n",board[i] + E[i * 2 + 1].flow);
}
}
else printf("NO\n");
}
return 0;
}