Zoj2314 feasible stream of the reactor cooling upper/Lower Bound Network

Source: Internet
Author: User
First, let's take a look at the question of lower-bound network streams. In addition to the capacity of each arc, the upstream and downstream network flow problem also specifies a lower limit, the traffic on the arc should not be less than the lower limit of the arc. We recommend an article http://wenku.baidu.com/view/0f3b691c59eef8c75fbfb35c.html this article introduces the solution to this kind of problem. After reading this article, it should be a piece of cake to solve this problem. Figure 1: Create an Arc Based on the edges shown in the figure, but the capacity is changed to c-l (L is the lower limit specified by this arc ). 2: Add Source Vertex S and sink vertex T. For each vertex, calculate the total lower limit of inbound in and the total lower limit of outbound out, and find in-out. If it is greater than 0, it is connected to the Source Vertex, And the capacity is poor. If it is less than 0, it is connected to the sink, and the capacity is the opposite of the difference. Find the maximum stream. If the stream is full, a feasible stream exists; otherwise, no.

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

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MAXN 220 #define MAXM 50000#define INF 0xFFFFFFstruct edge{int to,c,next;};edge e[MAXM];int head[MAXN],en,n,m;int dis[MAXN],gap[MAXN],st,ed;int l[MAXM];int flowin[MAXN],flowou[MAXN];void add(int a, int b, int c) {    e[en].to=b;    e[en].c=c;    e[en].next=head[a];    head[a]=en++;    e[en].to=a;    e[en].c=0;    e[en].next=head[b];    head[b]=en++;} int isap(int u,int flow) {    if(u==ed)return flow;    int j,mindis=n+1,t=flow,d;    for(j=head[u];j!=-1;j=e[j].next) {        int v=e[j].to,val=e[j].c;        if(val>0) {            if(dis[v]+1==dis[u]) {                if(t<e[j].c)                     d=t;else                     d=e[j].c;                d=isap(v,d);                e[j].c-=d,e[j^1].c+=d;                t-=d;                if(dis[st]>=n+2)                    return flow-t;                if(t==0)                     break;            }            if(dis[v]<mindis)                mindis=dis[v];        }    }    if(t==flow) {        --gap[dis[u]];        if (gap[dis[u]]==0)             dis[st]=n+2;        dis[u]=mindis+1;        ++gap[dis[u]];    }    return flow-t;}void solve(){int a,b,c;int full=0;scanf("%d%d",&n,&m);memset(head,-1,sizeof(head));memset(flowin,0,sizeof(flowin));memset(flowou,0,sizeof(flowou));en=0,st=0,ed=n+1;for(int i=0;i<m;i++){scanf("%d%d%d%d",&a,&b,&l[i],&c);flowou[a]+=l[i],flowin[b]+=l[i];add(a,b,c-l[i]);}for(int i=1;i<=n;i++){int temp=flowin[i]-flowou[i];if(temp>0){add(st,i,temp);full+=temp;}elseadd(i,ed,-temp);}memset(dis,0,sizeof(dis)),memset(gap,0,sizeof(gap));gap[0]=n+2;int ret=0; while(dis[st]<n+2)ret+=isap(st,INF);if(ret!=full){printf("NO\n");return;}elseprintf("YES\n");for(int i=0;i<m;i++) printf("%d\n",e[(i*2)^1].c+l[i]);}int main(){int t;scanf("%d",&t);while(t--){solve();}return 0;} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.