zoj2314(上下界网络流)

Description

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

      Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be fi,j ≤ ci,j where ci,j 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 li,j , thus it must be fi,j ≥ li,j. Given ci,j and li,j for all pipes, find the amount fi,j, 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 i,j and c i,j each. There is at most one pipe connecting any two nodes and 0 ≤ l i,j ≤ c i,j ≤ 10 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

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 Output

NO
YES
1
2
3
2
1
1


题意:给出一个有上下界的网络流,判断是否存在可行的循环流,如果存在输出“YES”并依次输出每条边的流量,否则输出“NO”。

算法:

重新构图如下:

(1)在原网络的基础上新增源点和汇点s,t,构造出新的只有上界的网络。

(2)对于原网络中的每条弧(u,v),在新网络中添加一条s->v 的容量为low[u][v]的弧,一条u->t 的容量为low[u][v]的弧,一条u->v的容量为up[u][v]-low[u][v] 的弧。

在重新构造的网络中,做s->t的最大流,如果网络流满流,则原网络存在可行的循环流。原图中每一条弧(u,v)对应的流量为新网络中(u,v)的流量加上low[u][v]。


这题是明显的模板题。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iomanip>
using namespace std;
typedef long long ll;
const   int oo=1e9;
/**oo 表示无穷大*/
const  int mm=200000;
/**mm 表示边的最大数量,记住要是原图的两倍,在加边的时候都是双向的*/
const  int mn=210;
/**mn 表示点的最大数量*/
int node,src,dest,edge;
/**node 表示节点数,src 表示源点,dest 表示汇点,edge 统计边数*/
int ver[mm],flow[mm],nex[mm];
int head[mn],work[mn],dis[mn],q[mn];
void prepare(int _node, int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<=node; ++i)head[i]=-1;
    edge=0;
}
void addedge( int u,  int v,  int c)
{
    ver[edge]=v,flow[edge]=c,nex[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,nex[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; ++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=nex[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)  return 1;
            }
    return 0;
}
int Dinic_dfs(  int u, int exp)
{
    if(u==dest)  return exp;
    for(int &i=work[u],v,tmp; i>=0; i=nex[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        while((delta=Dinic_dfs(src,oo)))ret+=delta;
    }
    return ret;
}
int low[20000],tu[20000];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        prepare(n+2,0,n+1);
        int s=0,bj=0;
        while(m--)
        {
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            addedge(0,b,c);
            addedge(a,n+1,c);
            low[bj]=c;
            tu[bj++]=edge;
            addedge(a,b,d-c);
            s+=c;
        }
        if(Dinic_flow()==s)
        {
            printf("YES\n");
            for(int i=0; i<bj; i++)
                printf("%d\n",flow[tu[i]^1]+low[i]);
        }
        else
            puts("NO");
    }
    return 0;
}

posted @ 2016-04-13 19:39  martinue  阅读(148)  评论(0编辑  收藏  举报