ZOJ 2314 无源汇可行流(输出方案)

Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

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

fi,1+fi,2+...+fi,N = f1,i+f2,i+...+fN,i

Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be fij <= cij where cij 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 lij, thus it must be fij >= lij.

Given cij and lij for all pipes, find the amount fij, satisfying the conditions specified above.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

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, lij and cij each. There is at most one pipe connecting any two nodes and 0 <= lij <= cij <= 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 Input

2

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 Input

NO

YES
1
2
3
2
1
1

大意:

一个没有源点汇点的图,每条边有最小和最大流量,流在图中循环。求是否存在符合要求(每条边的流量在最小和最大限制之间)的流,输出方案,spj。

题解:

将问题转化为有源点汇点的图

设超级源点S,超级汇点T

将一条流量边  a->b   [max ,  min]  (a到b,最大流量max,最小流量min)拆为三条边:

S->b  min  

a->T  min

a->b max-min

个人理解,对于这一条边,跑最大流的时候需要满足从b流出的流量为min,到a的流量为min。

如果跑完最大流后满流,则存在方案,因为从S的出边流量和到T的流量相等,都等于sigma(min)

如果满流,则最小流量条件能够满足。

/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
    return xx*ff;
}
inline int mymin(int xx,int yy)
{if(xx<yy)return xx;return yy;}
const int maxn=210;
int N,M,T,t1,t2,t3,t4,sum,ans;
int ss,tt,mp[1000010][2];
int lin[maxn],len;
struct edge{
    int y,next,flow;
}e[1000010];
inline void insert(int xx,int yy,int ff){
    e[++len].next=lin[xx];
    lin[xx]=len;
    e[len].y=yy;
    e[len].flow=ff;
}
inline void ins(int xx,int yy,int ff)
{insert(xx,yy,ff),insert(yy,xx,0);}
int q[maxn],head,tail,level[maxn];
bool makelevel(){
    memset(level,-1,sizeof(level));
    head=tail=0;
    q[head]=ss;
    level[ss]=0;
    for(;head<=tail;head++){
        for(int i=lin[q[head]];i;i=e[i].next)
            if(level[e[i].y]==-1&&e[i].flow){
                level[e[i].y]=level[q[head]]+1;
                q[++tail]=e[i].y;
            }
    }
    return level[tt]!=-1;
}
int max_flow(int x,int flow){
    if(x==tt)
        return flow;
    int d,maxflow=0;
    for(int i=lin[x];i&&maxflow<flow;i=e[i].next)
        if(level[e[i].y]==level[x]+1&&e[i].flow){
            d=max_flow(e[i].y,mymin(e[i].flow,flow-maxflow));
            if(d){
                maxflow+=d;
                e[i].flow-=d;
                if(i&1)
                    e[i+1].flow+=d;
                else
                    e[i-1].flow+=d;
            }
        }
    if(!maxflow)
        level[x]=-1;
    return maxflow;
}
void dinic(){
    ans=0;
    while(makelevel()){
        int d=1;
        while(d){
            d=max_flow(ss,1<<30);
            ans+=d;
        }
    }
    if(ans==sum){
        printf("YES
");
        for(int i=1;i<=M;i++)
            printf("%d
",e[mp[i][0]].flow+mp[i][1]);
    }
    else
        printf("NO

");
}
int main(){
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    T=read();
    while(T--){
        N=read(),M=read();
        ss=N+1,tt=ss+1;
        memset(lin,0,sizeof(lin));len=0;sum=0;
        for(int i=1;i<=M;i++){
            t1=read(),t2=read(),t3=read(),t4=read();
            ins(t1,t2,t4-t3);
            mp[i][0]=len;mp[i][1]=t3;
            ins(ss,t2,t3);
            ins(t1,tt,t3);
            sum+=t3;
        }
        dinic();
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/lzhAFO/p/8215469.html