您的位置:首页 > Web前端 > React

POJ 2314 Reactor Cooling 有上下限的最大流

2016-04-14 11:36 411 查看

题目描述:

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

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


题目分析:

n个点,m条边,其中每条边有一个上限流量,下限流量。如果能使每个点的流出流量与流入流量相等,输入YES并输出各边可行的流量,不行输出NO。

有下限的网络流,需要将下限调制0,这样会导致总流量不守恒,通过与超级源点与超级汇点的流量调节平衡。

具体解释见代码。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MAXN = 2020;

struct node
{
int v;
int w;
int next;
int num;//本题需要记录每条边的标号,最后输出用
}mp[MAXN<<5];
int id;
int q[MAXN];
int level[MAXN];
int head[MAXN];

void init()
{
memset(head,-1,sizeof(head));
id=0;
}

void addedge(int u,int v,int w,int i)
{
mp[id].v=v;
mp[id].w=w;
mp[id].next=head[u];
mp[id].num=i;//标号
head[u]=id++;

mp[id].v=u;
mp[id].w=0;
mp[id].next=head[v];
mp[id].num=0;
head[v]=id++;
}

int bfs(int s, int t) //构建层次网络
{
memset(level,0,sizeof(level));
level[s]=1;
int front=0,rear=1;
q[front]=s;
while(front < rear)
{
int x=q[front++];
if(x==t) return 1;
for(int e=head[x]; e!=-1; e=mp[e].next)
{
int v=mp[e].v, f=mp[e].w;
if(!level[v] && f)
{
level[v]=level[x]+1;
q[rear++]=v;
}
}
}
return 0;
}

int dfs(int u,int maxf,int t)
{
if(u==t) return maxf;
//int ret=0;
for(int e=head[u]; e!=-1; e=mp[e].next)
{
int v=mp[e].v, f;
if(level[u]+1==level[v] && mp[e].w && (f=dfs(v,min(mp[e].w,maxf),t)>0))
{
mp[e].w-=f;
mp[e^1].w+=f;
return f;
}
}
return 0;
}

void dinic(int s, int t) //s源点 t汇点
{
//int ans = 0;
while(bfs(s,t))
dfs(s,INF,t);
//return ans;
}

int main()
{
int n,m;
int sum[MAXN<<5];//总流量
int dedge[MAXN<<5];//每条边最低流量
int ans[MAXN<<5];
while(~scanf("%d%d",&n,&m))
{
init();
int s=0,t=n+1;
memset(sum,0,sizeof(sum));
memset(dedge,0,sizeof(dedge));
memset(ans,0,sizeof(ans));
for(int i=1; i<=m; i++)
{
int u,v,down,up;
scanf("%d%d%d%d",&u,&v,&down,&up);
addedge(u,v,up-down,i);
sum[u]-=down;
sum[v]+=down;//由于我们使每条边下限设为0 需要把这个值记录下来保证总流量不变
dedge[i]=down;//第i条边的最小流量
}
for(int i=1; i<=n; i++)
{
if (sum[i]>0) addedge(s,i,sum[i],0);//若改变后i点总流量大于0,设置一条与源点的边
if (sum[i]<0) addedge(i,t,-sum[i],0);//同理
}
dinic(s,t);
bool f=1;
for(int i=head[s]; i!=-1; i=mp[i].next)
{
if (mp[i].w>0) {f=0; break;}
}
if (!f) puts("NO");
else
{
puts("YES");
for(int i=0; i<id; i++) ans[mp[i].num]=mp[i^1].w;//每个点的流量
for(int i=1; i<=m; i++)
printf("%d\n",ans[i]+dedge[i]);//加上被减去的最小流量
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: