[ZOJ 2314]Reactor Cooling(有上下界的網絡流)

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

Solution


題意是說有n個節點m根管道,每個管道有方向且有流量的上界與下界,問是否有可行流,若可行則輸出每根管道的流量。
有上下界的網絡流·裸
本來是在做ZOJ 3229的,做着做着發現自己並沒有領會有上下界網絡流的奧義,於是return到這裏。。

在無源無匯的情況下,我們人爲製造一個源和匯
記錄一個in數組,就是流入該點的下界減去流出的下界
如果in[i]>0 則連一條源到這點 容量爲 in[i] 的邊
如果in[i]<0 則連一條這點到匯 容量爲|in[i]|的邊
於是在加邊的時候,cap應該爲上界-下界(暫且稱作自由流),就是假裝下界已經流滿了
然後做一次網絡流
若源連出的所有邊都已滿流,則說明可行
以上內容的證明詳見論文周源:一種簡易的方法求解流量有上下界的網絡中網絡流問題

然後要求每根管子的流量,就是下界+自由流,而自由流應該是Edges[((i-1)*2)^1].cap
之所以是(i-1)*2是因爲邊是從cnt=0開始計數的,異或是它的反向邊,反向邊cap的增加即該邊cap的減少,就是流過的流量

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<string>
#define Min(a,b) (a<b?a:b)
#define INF 0x3f3f3f3f
using namespace std;
int T;
int n,m,s,t;
int head[250],cnt=0,in[250],level[250],low[40005];
struct Node{
    int next,to,cap;
}Edges[80005];
void addedge(int u,int v,int c)
{
    Edges[cnt].cap=c;
    Edges[cnt].to=v;
    Edges[cnt].next=head[u];
    head[u]=cnt;
    cnt++;
}
bool bfs()
{
    memset(level,-1,sizeof(level));
    queue<int>q;
    level[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        for(int i=head[u];~i;i=Edges[i].next)
        {
            int v=Edges[i].to;
            if(level[v]==-1&&Edges[i].cap)
            {
                level[v]=level[u]+1;
                q.push(v);
            }
        }
        q.pop();
    }
    if(level[t]==-1)return false;
    return true;
}
int dfs(int u,int flow)
{
    if(u==t)return flow;
    int Maxu=0,d=0;
    for(int i=head[u];~i&&Maxu<flow;i=Edges[i].next)
    {
        int v=Edges[i].to;
        if(level[v]==level[u]+1&&Edges[i].cap)
        {
            d=dfs(v,Min(flow-Maxu,Edges[i].cap));
            Edges[i].cap-=d;
            Edges[i^1].cap+=d;
            Maxu+=d;
        }
    }
    if(!Maxu)level[u]=-1;
    return Maxu;
}
bool judge()
{
    while(bfs())
    {
        while(dfs(s,INF));
    }
    for(int i=head[s];~i;i=Edges[i].next)
    {
        if(Edges[i].cap)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    scanf("%d",&T);
    while(T--&&~scanf("%d%d",&n,&m))
    {
        cnt=0;
        memset(low,0,sizeof(low));
        memset(head,-1,sizeof(head));
        memset(in,0,sizeof(in));
        for(int i=1;i<=m;i++)
        {
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            addedge(a,b,d-c);
            addedge(b,a,0);
            low[i]=c;
            in[a]-=c;
            in[b]+=c;
        }
        s=0;t=n+1;
        for(int i=1;i<=n;i++)
        {
            if(in[i]<0)
            {
                addedge(i,t,-in[i]);
                addedge(t,i,0);
            }
            else if(in[i]>0)
            {
                addedge(s,i,in[i]);
                addedge(i,s,0);
            }
        }
        if(judge())
        {
            printf("YES\n");
            for(int i=1;i<=m;i++)
            {
                printf("%d\n",Edges[((i-1)*2)^1].cap+low[i]);
            }
        }
        else printf("NO\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章