zoj2314 Reactor Cooling [无源汇有上下界可行流]

in 题目 with 0 comment

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:
$$ f_{(i,1)}+f_{(i,2)}+...+f_{(i,N)} = f_{(1,i)}+f_{(2,i)}+...+f_{(N,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 $ f_{(i,j)} \ge l_{(i,j)}$.

Given $c_{(i,j)}$ and $l_{(i,j)}$ for all pipes, find the amount $f_{(i,j)}$ , 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 \le N \le 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 \le l_{(i,j)} \le c_{(i,j)} \le 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

无源无汇有上下界网络流裸题,建图方法很简单:

  1. 设 $D(u)=\sum_{v}b(u,v)-\sum_{w}b(w,u)$,即D(u)表示点u流出的最小流量和与流入的最小流量和之差
  2. 对于原图中的边$u\rightarrow v$,在新图中的权值变为$(0,w-b[u])$,即流量随意的自由流
  3. 指定超级源s=0,超级汇t=n+1
  4. 若图中节点u的$D(u)<0$,说明此点还需要流入D(u)的自由流,所以$u\rightarrow t$连边,权值为-D(u)
  5. 若图中节点u的$D(u)>0$,说明此点还需要流出D(u)的自由流,所以$s\rightarrow u$连边,权值为D(u)

具体可以参考黄学长的博客


Code

#include<bits/stdc++.h>
#define maxn 250
#define maxe 50010
#define INF 0x3f3f3f3f
#define DEBUG
using namespace std;
int n,m,s,t,cnt;
int b[maxe],in[maxn];
int head[maxn],dis[maxn];

struct edge
{
    int to,cap,next;
    edge(int to=0,int cap=0,int next=0):to(to),cap(cap),next(next) {}
}e[maxe];

inline int read()
{
    char ch;
    int read=0,sign=1;
    do
        ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-');
    if(ch=='-') sign=-1,ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        read=read*10+ch-'0';
        ch=getchar();
    }
    return sign*read;
}

inline void ins(int u,int v,int w)
{
    e[++cnt]=edge(v,w,head[u]);
    head[u]=cnt;
}

inline void insert(int u,int v,int w)
{ ins(u,v,w);ins(v,u,0); }

void init()
{
    n=read(),m=read();
    s=0,t=n+1,cnt=1;
    memset(head,0,sizeof(head));
    memset(in,0,sizeof(in));
}

void build_graph()
{
    for(int i=1;i<=m;++i)
    {
        int u=read(),v=read(),w;
        b[i]=read(),w=read();
        in[u]-=b[i],in[v]+=b[i];
        insert(u,v,w-b[i]); 
    }
    for(int i=1;i<=n;++i)
    {
        if(in[i]<0) insert(i,t,-in[i]);
        if(in[i]>0) insert(s,i,in[i]);
    }
}

bool bfs()
{
    memset(dis,-1,sizeof(dis));
    queue<int> q;
    q.push(s);
    dis[s]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        int i=head[u];
        while(i)
        {
            if(e[i].cap && dis[e[i].to]<0)
            {
                dis[e[i].to]=dis[u]+1;
                q.push(e[i].to);
            }
            i=e[i].next;
        }
    }
    if(dis[t]==-1) return 0;
    return 1;
}

int dfs(int x,int flow)
{
    if(x==t) return flow;
    int i=head[x],used=0,w;
    while(i)
    {
        if(e[i].cap && dis[e[i].to]==dis[x]+1)
        {
            w=flow-used;
            w=dfs(e[i].to,min(w,e[i].cap));
            e[i].cap-=w;
            e[i^1].cap+=w;
            used+=w;
            if(used==flow) return flow;
        }
        i=e[i].next;
    }
    if(!used) dis[x]=-1;
    return used;
}

void dinic()
{
    int ans=0;
    while(bfs()) ans+=dfs(s,INF);
}

bool judge()
{
    for(int i=head[s];i;i=e[i].next)
        if(e[i].cap) return 0;
    return 1;
}

void output()
{
    if(!judge()) printf("NO\n");
    else
    {
        printf("YES\n");
        for(int i=1;i<=m;++i) 
            printf("%d\n",b[i]+e[(i<<1)^1].cap);
        printf("\n");
    }
}

int main()
{
    int T=read();
    while(T--)
    {
        init();
        build_graph();
        dinic();
        output();
    } 
    return 0;
} 

扫描二维码,在手机上阅读!
Responses