ZOJ 2314/SGU194 Reactor Cooling, zojsgu194

Source: Internet
Author: User

ZOJ 2314/SGU194 Reactor Cooling, zojsgu194


Passive sink point upstream and downstream feasible stream problems .....


Graph creation:

For an edge u ---> v low (u, v) high (u, v) connected edge u ---> v high (u, v)-low (u, v) it becomes a network flow problem with no upper or lower boundaries.

However, this does not necessarily satisfy the low relationship, so I add low .....

Set free stream g (u, v) = high (u, v)-low (u, v)

The traffic at each vertex is composed of free flow g and lower flow low ....




Variant:




We can see that the inbound and outbound traffic of each point is not necessarily balanced...


We use an array low to record the lower-bound traffic of each vertex. When low [I] <0, the lower-bound flow of inbound traffic is smaller than the lower-bound flow of outbound traffic, that is

The outbound traffic is less. We create a super sink point to connect an I ---> the T capacity is-Low [I] edge.


When Low [I]> 0, the inbound traffic is less. Create a super source node to connect to an edge of S ---> The I capacity is Low [I]... the graph is created.


A feasible stream must be the largest stream from the supplementary source to the supplementary sink. In addition, the edge of the source point must be full-stream.


Therefore, when the maximum stream is run, the system determines whether the connected edge of the source node is full. If the stream is full, it indicates that a feasible stream exists. If the stream is not full, no feasible stream exists.


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


Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #1



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=100005;const int INF=0x3f3f3f3f;struct Edge{    int to,next,cap,flow;}edge[1000000];int Size,Adj[maxn];int gap[maxn],dep[maxn],pre[maxn],cur[maxn];void init(){    Size=0;    memset(Adj,-1,sizeof(Adj));}void add_edge(int u,int v,int w,int rw=0){    edge[Size].to=v;edge[Size].cap=w;edge[Size].next=Adj[u];    edge[Size].flow=0;Adj[u]=Size++;    edge[Size].to=u;edge[Size].cap=rw;edge[Size].next=Adj[v];    edge[Size].flow=0;Adj[v]=Size++;}int sap(int start,int end,int N){    memset(gap,0,sizeof(gap));    memset(dep,0,sizeof(dep));    memcpy(cur,Adj,sizeof(Adj));        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;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;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;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=Adj[u];~i;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 n,m;int in[maxn],low[maxn];int main(){    int T_T;    scanf("%d",&T_T);    while(T_T--)    {        /// INIT        scanf("%d%d",&n,&m);        init();        memset(in,0,sizeof(in));        memset(low,0,sizeof(low));        int a,b,c,d;        for(int i=0;i<m;i++)        {            scanf("%d%d%d%d",&a,&b,&c,&d);            in[a]-=c;in[b]+=c;            low[i]=c;            add_edge(a,b,d-c);        }        for(int i=1;i<=n;i++)        {            if(in[i]<0) add_edge(i,n+1,-in[i]);            if(in[i]>0) add_edge(0,i,in[i]);        }        sap(0,n+1,n+2);        bool flag=true;        for(int i=Adj[0];~i;i=edge[i].next)        {            if(edge[i].cap!=edge[i].flow)            {                flag=false; break;            }        }        if(flag==true)        {            puts("YES");            for(int i=0;i<m;i++)            {                printf("%d\n",edge[2*i].flow+low[i]);            }        }        else        {            puts("NO");        }    }    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.