Reactor Cooling - sgu 194 无源汇可行流

前端之家收集整理的这篇文章主要介绍了Reactor Cooling - sgu 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 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 #14 61 2 1 22 3 1 23 4 1 24 1 1 21 3 1 24 2 1 2Test #24 61 2 1 32 3 1 33 4 1 34 1 1 31 3 1 34 2 1 3
Output
Test #1

NO

Test #2

YES
1
2
3
2
1
1

题意:给定一个网络图每条边的流量限制l<=f<=r,求是否有可行流,如果有可行流的话,这些边的流量各是多少。

思路:这个属于无源汇可行流,具体参考http://www.jb51.cc/article/p-pfhpkxbg-be.html

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int u,v,flow,next;
}edge[100010];
struct node2
{
    int u,l,r;
}arr[40010];
int n,m,Head[210],d[210],s,t,tot,INF=1e9;
int ans[210][210];
queue<int> qu;
void add(int u,int v,int f)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].flow=f;
    edge[tot].next=Head[u];
    Head[u]=tot++;
}
int bfs()
{
    int i,u,v;
    memset(d,-1,sizeof(d));
    while(!qu.empty())
      qu.pop();
    qu.push(s);
    d[s]=0;
    while(!qu.empty())
    {
        u=qu.front();
        qu.pop();
        for(i=Head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].v;
            if(edge[i].flow>0 && d[v]==-1)
            {
                d[v]=d[u]+1;
                if(v==t)
                  return 1;
                qu.push(v);
            }
        }
    }
    return 0;
}
int dfs(int u,int f)
{

    if(u==t || f==0)
      return f;
    int ret=0,i,k,v;
    for(i=Head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].v;
        if(edge[i].flow>0 && d[v]==d[u]+1)
        {
            k=dfs(v,min(edge[i].flow,f));
            edge[i].flow-=k;
            edge[i^1].flow+=k;
            f-=k;
            ret+=k;
            if(f==0)
              break;
        }
    }
    d[u]=-1;
    return ret;
}
int main()
{
    int i,k=0,k2,r;
    scanf("%d%d",&n,&m);
    s=n+1;
    t=n+2;
    memset(Head,sizeof(Head));
    memset(ans,sizeof(ans));
    tot=0;
    for(i=1;i<=m;i++)
    {
        scanf("%d%d%d%d",&u,&v,&l,&r);
        arr[i].u=u;arr[i].v=v;arr[i].l=l;arr[i].r=r;
        add(u,r-l);
        add(v,0);
        add(u,l);
        add(t,0);
        add(s,l);
        add(v,0);
        k+=l;
        ans[u][v]=l;
    }
    k2=0;
    while(bfs())
    {
        k2+=dfs(s,INF);
        //printf("k2=%d\n",k2);
    }
    if(k==k2)
    {
        printf("YES\n");
        for(i=1;i<tot;i+=2)
           ans[edge[i].v][edge[i].u]+=edge[i].flow;
        for(i=1;i<=m;i++)
           printf("%d\n",ans[arr[i].u][arr[i].v]);
    }
    else
      printf("NO\n");
}

猜你在找的React相关文章