[SGU 194] Reactor Cooling, sgureactor

Source: Internet
Author: User

[SGU 194] Reactor Cooling, sgureactor
194. Reactor Cooling

Time limit per test: 0.5 sec.
Memory limit per test: 65536 KB
Input: standard
Output: standard

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 f ij, (put f ij = 0 if there is no pipe from node I to node j), for each I the following condition must hold:

Sum (j = 1. N, f ij) = sum (j = 1. N, f ji)

Each pipe has some finite capacity, therefore for each I and j connected by the pipe must be f ij ≤ c ij where c ij 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 l ij, thus it must be f ij ≥ l ij.

Given c ij and l ij for all pipes, find the amount f ij, satisfying the conditions specified above.

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, l ij and c ij each. there is at most one pipe connecting any two nodes and 0 ≤ l ij ≤ c ij ≤ 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 test (s)

Input
Test #1

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

Test #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

Output
Test #1

NO

Test #2

YES
1
2
3
2
1
1

Passive sink has upstream and downstream feasible streams.

For details, see Network Flow Problems with upper and lower limits.

#include <iostream>#include <cstring>#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#define M 500005#include <queue>#define inf 0x3f3f3f3fusing namespace std;int h[M],id[M],cur[M],tot,v[M],d[M],ans[M],n,m,s,t;struct edge{    int from,to,cap,flow,ne;}E[M];void Addedge(int from,int to,int cap){    E[++tot]=(edge){from,to,cap,0,h[from]};    h[from]=tot;    E[++tot]=(edge){to,from,0,0,h[to]};    h[to]=tot;}void Clear(){    tot=1;    for (int i=0;i<=n+1;i++)        h[i]=0;}int bfs(){    for (int i=s;i<=t;i++)        v[i]=0;    v[s]=1;    d[s]=0;    queue<int> q;    q.push(s);    while (!q.empty())    {        int x=q.front();        q.pop();        for (int i=h[x];i;i=E[i].ne)        {            edge e=E[i];            if (!v[e.to]&&e.cap>e.flow)            {                v[e.to]=1;                d[e.to]=d[x]+1;                q.push(e.to);            }        }    }    return v[t];}int dfs(int x,int a){    if (x==t||!a) return a;    int flow=0;    for (int &i=cur[x];i;i=E[i].ne)    {        edge &e=E[i];        if (d[x]+1!=d[e.to]) continue;        int f=dfs(e.to,min(a,e.cap-e.flow));        if (f>0)        {            e.flow+=f;            E[i^1].flow-=f;            flow+=f;            a-=f;            if (!a) break;        }    }    return flow;}int dinic(){    int flow=0;    while (bfs())    {        for (int i=s;i<=t;i++)            cur[i]=h[i];        flow+=dfs(s,inf);    }    return flow;}int main(){    while (scanf("%d%d",&n,&m)!=EOF)    {        s=0,t=n+1;        int jud=0;        Clear();        for (int i=1;i<=m;i++)        {            int x,y,down,up;            scanf("%d%d%d%d",&x,&y,&down,&up);            Addedge(x,y,up-down);            id[i]=tot-1;            jud+=down;            Addedge(s,y,down);            Addedge(x,t,down);            ans[i]=down;        }        if (dinic()!=jud)        {            printf("NO\n");            continue;        }        else        {            printf("YES\n");            for (int i=1;i<=m;i++)                printf("%d\n",ans[i]+E[id[i]].flow);        }    }    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.