【SGU 194】 Reactor Cooling

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

无源汇有上下界可行流。

详见《有上下界的网络流问题

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#define M 500005
#include <queue>
#define inf 0x3f3f3f3f
using 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值