无源汇最大流 SGU 194 Reactor Cooling

前端之家收集整理的这篇文章主要介绍了无源汇最大流 SGU 194 Reactor Cooling前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

194. Reactor Cooling

time limit per test: 1 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 ijwhere c ijis 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 ijand l ijfor 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 ijand c ijeach. There is at most one pipe connecting any two nodes and 0 ≤ l ij≤ c ij≤ 10 5for 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
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

刚开始看有上下界的网络流,借助对于无源汇的最大流的题解,就先写这道题了。

思路:

上界用Ci表示,下界用Bi表示。

下界是必须流满,那么对于每一条边,去掉下界后,其自由流为Ci– Bi

每一个点流进来的流=流出去的流

对于每一个点i,令

Mi= sum(i点所有流进的下界流)– sum(i点所有流出的下界流)

如果Mi大于0,代表此点必须还要流出去Mi的自由流,那么我们从源点连一条Mi的边到该点。

如果Mi小于0,代表此点必须还要流进来Mi的自由流,那么我们从该点连一条Mi的边到汇点。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mm=100000;
const int mn=222;
const int oo=1000000000;
int node,s,t,edge;
int to[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
int M[mn],B[mm],C[mm];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void init(int nn,int ss,int tt)
{
    node=nn,s=ss,t=tt,edge=0;
    for(int i=0;i<node;++i) head[i]=-1;
}
inline void add(int u,int v,int c1,int c2=0)
{
    to[edge]=v,flow[edge]=c1,next[edge]=head[u],head[u]=edge++;
    to[edge]=u,flow[edge]=c2,next[edge]=head[v],head[v]=edge++;
}
bool bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i) dis[i]=-1;
    dis[q[r++]=s]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=to[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==t) return 1;
            }
    return 0;
}
int dfs(int u,int maxf)
{
    if(u==t) return maxf;
    for(int &i=work[u],tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=to[i]]==dis[u]+1&&(tmp=dfs(v,min(maxf,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int dinic()
{
    int i,ret=0,delta;
    while(bfs())
    {
        for(i=0;i<node;++i) work[i]=head[i];
        while(delta=dfs(s,oo))ret+=delta;
    }
    return ret;
}
int main()
{
    int i,n,m,v;
    while(~scanf("%d%d",&n,&m))
    {
        init(n+2,n+1);
        memset(M,sizeof(M));
        int sflow=0,tflow=0;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&u,&v,&B[i],&C[i]);
            add(u,C[i]-B[i]);
            M[v]+=B[i];
            M[u]-=B[i];
        }
        int tmp=edge;
        for(i=1;i<=n;i++)
        {
        if(M[i]>=0)
        {
            add(s,i,M[i]);
            sflow+=M[i];
        }
        else {
            add(i,-M[i]);
            tflow+=-M[i];
        }
        }
        //cout<<sflow<<" "<<tflow<<endl;
        if(dinic()==sflow) {
            puts("YES");
            for(i=0;i<tmp;i+=2)
            printf("%d\n",C[i/2]-flow[i]);
        }
        else puts("NO");
    }
    return 0;
}

猜你在找的React相关文章