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

Codeforces Gym 100199 B Reactor Cooling

2017-02-21 17:31 330 查看
Problem B. Reactor Cooling Input file: cooling.in Output file:

cooling.out Time limit: 1 second 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 f ij , (put f ij

= 0 if there is no pipe from node i to node j ), for each i the following condition must hold: N ∑ j

=1 f ij

= N ∑ j

=1 f ji Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be f ij ≤ c ij where c ij 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 l ij , thus it must be f ij ≥ l ij . Given c ij and l ij for all pipes, find the amount f ij , 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 ij and c ij each. There is at most one pipe

connecting any two nodes and 0 ≤ l ij ≤ c ij ≤ 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.

对于每条边u−>v:[b,c],先强制流满b,也就是剩下c−b,然后连s−>v:b、u−>t:b。跑s−>t最大流,如果满流,代表存在别的流量满足x−>u:b、v−>y:b,而且撤去s、t之后这些流量多出来无法平衡,正好填满u−>v:b。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int s=205,t=206,oo=0x3f3f3f3f;
int fir[210],ne[100010],to[100010],w[100010],
ws[210],wt[210],low[100010],up[100010],que[210],dep[210],
n,m,num;
void add(int u,int v,int x)
{
num++;
ne[num*2]=fir[u];
fir[u]=num*2;
to[num*2]=v;
w[num*2]=x;
ne[num*2+1]=fir[v];
fir[v]=num*2+1;
to[num*2+1]=u;
w[num*2+1]=0;
}
bool bfs()
{
int hd=1,tl=1,u,v;
que[1]=s;
memset(dep,0,sizeof(dep));
dep[s]=1;
while (hd<=tl)
{
u=que[hd++];
for (int i=fir[u];i;i=ne[i])
if (w[i]&&!dep[v=to[i]])
{
dep[v]=dep[u]+1;
que[++tl]=v;
}
}
return dep[t];
}
int dfs(int u,int lim)
{
int v,ret=0,x;
if (u==t) return lim;
for (int i=fir[u];i&&ret<lim;i=ne[i])
if (w[i]&&dep[v=to[i]]==dep[u]+1)
{
x=dfs(v,min(w[i],lim-ret));
ret+=x;
w[i]-=x;
w[i^1]+=x;
}
if (!ret) dep[u]=0;
return ret;
}
void check()
{
for (int i=1;i<=t;i++)
for (int j=fir[i];j;j=ne[j])
if (w[j]) printf("%d:%d->%d:%d\n",j,i,to[j],w[j]);
}
int main()
{
freopen("cooling.in","r",stdin);
freopen("cooling.out","w",stdout);
int u,v,sum=0,ans=0;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&u,&v,&low[i],&up[i]);
ws[v]+=low[i];
wt[u]+=low[i];
sum+=low[i];
add(u,v,up[i]-low[i]);
}
for (int i=1;i<=n;i++)
{
add(s,i,ws[i]);
add(i,t,wt[i]);
}
//check();
while (bfs()) ans+=dfs(s,oo);
//check();
if (ans==sum)
{
printf("YES\n");
for (int i=1;i<=m;i++) printf("%d\n",up[i]-w[i*2]);
}
else printf("NO\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  可行流