ZOJ2314 Reactor Cooling 有上下界网络的可行流

前端之家收集整理的这篇文章主要介绍了ZOJ2314 Reactor Cooling 有上下界网络的可行流前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
第一道上下界网络流的题目,纪念一下。
上下界网络流问题,除了每一条弧有一个容量,还规定了一个下限,规定弧上的流量不能小于这个弧的下限
推荐一篇文章http://wenku.baidu.com/view/0f3b691c59eef8c75fbfb35c.html这片文章介绍了这类问题的解决思想,看完了这片文章解决这道题目应该是小菜一碟了。
建图:
1:按照图中给出的边建立相应的弧,只是容量变为c-l (l为该弧所规定的下限)。
2:增加源点s和汇点t。对于每一个点统计流入的总下限in,和流出的总下限out,求出in-out,如果大于0则与源点相连,容量为差。如果小于0,则与汇点相连,容量为差的相反数。
求出最大流,如果是满流,则存在可行流,否则不存在。
Reactor Cooling

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:

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define MAXN 220 
#define MAXM 50000
#define INF 0xFFFFFF

struct edge
{
	int to,c,next;
};

edge e[MAXM];
int head[MAXN],en,n,m;
int dis[MAXN],gap[MAXN],st,ed;
int l[MAXM];
int flowin[MAXN],flowou[MAXN];

void add(int a,int b,int c) 
{
    e[en].to=b;
    e[en].c=c;
    e[en].next=head[a];
    head[a]=en++;
    e[en].to=a;
    e[en].c=0;
    e[en].next=head[b];
    head[b]=en++;
}
 

int isap(int u,int flow) 
{
    if(u==ed)
		return flow;
    int j,mindis=n+1,t=flow,d;
    for(j=head[u];j!=-1;j=e[j].next) 
	{
        int v=e[j].to,val=e[j].c;
        if(val>0)
		 {
            if(dis[v]+1==dis[u])
			 {
                if(t<e[j].c) 
                    d=t;
				else 
                    d=e[j].c;
                d=isap(v,d);
                e[j].c-=d,e[j^1].c+=d;
                t-=d;
                if(dis[st]>=n+2)
                    return flow-t;
                if(t==0) 
                    break;
            }
            if(dis[v]<mindis)
                mindis=dis[v];
        }
    }
    if(t==flow) 
	{
        --gap[dis[u]];
        if (gap[dis[u]]==0) 
            dis[st]=n+2;
        dis[u]=mindis+1;
        ++gap[dis[u]];
    }
    return flow-t;
}

void solve()
{
	int a,b,c;
	int full=0;
	scanf("%d%d",&n,&m);
	memset(head,-1,sizeof(head));
	memset(flowin,sizeof(flowin));
	memset(flowou,sizeof(flowou));
	en=0,st=0,ed=n+1;
	for(int i=0;i<m;i++)
	{
		scanf("%d%d%d%d",&a,&b,&l[i],&c);
		flowou[a]+=l[i],flowin[b]+=l[i];
		add(a,c-l[i]);
	}
	for(int i=1;i<=n;i++)
	{
		int temp=flowin[i]-flowou[i];
		if(temp>0)
		{
			add(st,i,temp);
			full+=temp;
		}
		else
			add(i,ed,-temp);
	}
	memset(dis,sizeof(dis)),memset(gap,sizeof(gap));
	gap[0]=n+2;
	int ret=0; 
	while(dis[st]<n+2)
		ret+=isap(st,INF);
	if(ret!=full)
	{
		printf("NO\n");
		return;
	}
	else	
		printf("YES\n");
	for(int i=0;i<m;i++) printf("%d\n",e[(i*2)^1].c+l[i]);
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		solve();
	}
	return 0;
} 

猜你在找的React相关文章