题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314

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.

题意:很裸的题意,仔细读一下就明白了,就是一个无源汇的上下界网络流。

解法:无源汇的上下界网络流。下面简单介绍一下无源汇的上下界网络流构图方法,如有不当或不足之处,感谢提出。

针对边的构造  :

增加新源点from和汇点to,对于原图中任意一条边u->v,上下界流量分别为b,c,构造新图时,u->v的上界为c-b,新源点from->v上界为b,u->新汇点to上界为b。这样,就去掉了流量下界的控制。

参考书籍:《挑战程序设计竞赛》

针对点的构造  :

增加新源点from和汇点to,原图中的边u->v的上界为c-b(和上面一样),对于原图中任意一个点u,统计所有流入u点的流量下界和 in 和经过u点流出的所有流量下界和out ,如果 in > out ,那么就连一条边from->u上界为in-out,否则就连一条边u->to上界为out-in。

参考论文:《一种简易的方法求解流量有上下界的网络中网络流问题》

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; struct Edge
{
int to,cap,next;
int w;
Edge (){}
Edge (int to2,int cap2,int next2,int w2){to=to2,cap=cap2,next=next2,w=w2;}
}edge[M*];
int head[maxn],edgenum;
int n,m,from,to,vnum;
int id[M],c[M]; void add(int u,int v,int cap)
{
edge[edgenum].to=v;
edge[edgenum].cap=cap;
edge[edgenum].w=cap;
edge[edgenum].next=head[u];
head[u]=edgenum ++ ; edge[edgenum].to=u;
edge[edgenum].cap=;
edge[edgenum].w=cap;
edge[edgenum].next=head[v];
head[v]=edgenum ++ ;
} int level[maxn];
int gap[maxn];
void bfs(int to)
{
memset(level,-,sizeof(level));
memset(gap,,sizeof(gap));
level[to]=;
gap[level[to] ]++;
queue<int> Q;
Q.push(to);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].to;
if (level[v] != -) continue;
level[v]=level[u]+;
gap[level[v] ]++;
Q.push(v);
}
}
} int pre[maxn];
int cur[maxn];
int SAP(int from,int to)
{
bfs(to);
memset(pre,-,sizeof(pre));
memcpy(cur,head,sizeof(head));
int u=pre[from]=from,flow=,aug=inf;
gap[]=vnum;
while (level[from]<vnum)
{
bool flag=false;
for (int &i=cur[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].to;
if (edge[i].cap> && level[u]==level[v]+)
{
flag=true;
aug=min(aug,edge[i].cap);
pre[v]=u;
u=v;
if (v==to)
{
flow += aug;
for (u=pre[v] ;v!=from ;v=u,u=pre[u])
{
edge[cur[u] ].cap -= aug;
edge[cur[u]^ ].cap += aug;
}
aug=inf;
}
break;
}
}
if (flag) continue;
int minlevel=vnum;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].to;
if (edge[i].cap> && level[v]<minlevel)
{
minlevel=level[v];
cur[u]=i;
}
}
if (--gap[level[u] ]==) break;
level[u]=minlevel+;
gap[level[u] ]++;
u=pre[u];
}
return flow;
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int u,v,w;
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
edgenum=;
from= ;to=n+ ;
vnum=n+;
int sum=;
for (int i= ;i<=m ;i++)
{
scanf("%d%d%d%d",&u,&v,&c[i],&w);
sum += c[i];
id[i]=edgenum;
add(u,v,w-c[i]);
add(from,v,c[i]);
add(u,to,c[i]);
}
int Maxflow=SAP(from,to);
int flag=;
for (int i=head[from] ;i!=- ;i=edge[i].next)
if (edge[i].cap) {flag=;break; }
if (flag) printf("NO\n");
else
{
printf("YES\n");
for (int i= ;i<=m ;i++)
printf("%d\n",edge[id[i]^ ].cap+c[i]);
//printf("%d\n",edge[id[i] ].w-edge[id[i] ].cap+c[i]);
}
if (t) printf("\n");
}
return ;
}

zoj 2314 Reactor Cooling 网络流的更多相关文章

  1. ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...

  2. ZOJ 2314 Reactor Cooling

    Reactor Cooling Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  3. zoj 2314 Reactor Cooling (无源汇上下界可行流)

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

  4. ZOJ 2314 Reactor Cooling(无源汇上下界网络流)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题意: 给出每条边流量的上下界,问是否存在可行流,如果存在则输出. ...

  5. ZOJ 2314 Reactor Cooling 带上下界的网络流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的, ...

  6. ZOJ 2314 Reactor Cooling [无源汇上下界网络流]

    贴个板子 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...

  7. ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...

  8. ZOJ 2314 Reactor Cooling | 无源汇可行流

    题目: 无源汇可行流例题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题解: 证明什么的就算了,下面给出一种建图方式 ...

  9. Zoj 2314 Reactor Cooling(无源汇有上下界可行流)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意:    给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...

随机推荐

  1. Asp.net页面跳转Session丢失问题

    原本去年在做项目时,写好的一记篇博客分享给大家. Asp.net页面跳转Session丢失问题   编写人:CC阿爸 2014-4-2 l  近来在做泛微OA与公司自行开发的系统集成登录的问题.在使用 ...

  2. 用,隔开sql临时表

    IF OBJECT_ID('[kkd].[proc_kkd_GetAutoExamineBid]') IS NOT NULL BEGIN DROP PROC [kkd].[proc_kkd_GetAu ...

  3. Spark RDD的依赖解读

    在Spark中, RDD是有依赖关系的,这种依赖关系有两种类型 窄依赖(Narrow Dependency) 宽依赖(Wide Dependency) 以下图说明RDD的窄依赖和宽依赖 窄依赖 窄依赖 ...

  4. 一款兼容IE6并带有多图横向滚动的jquery特效

    一款兼容IE6并带有多图横向滚动的jquery特效,自动切换多个图片的jquery特效效果, 为大家分享这个的原因是,这款特效在兼容IE6上面很完美,实用性就广很多了. 适用浏览器:IE6.IE7.I ...

  5. Service(一)----->简单计算

    xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  6. Perl 随机数和随机密码的产生

    Perl有着强大的随机数产生函数rand(),下面的代码详细介绍其应用 #!/usr/bin/perl #  use strict;   use warnings; # 0~1之间    $rando ...

  7. rails 学习笔记

    bundle package   #保存gem到 vendor/cache bundle install –local  从cache从安装 升级rails bundle config –delete ...

  8. 刀哥多线程之一次性代码gcd-11-once

    一次性执行 有的时候,在程序开发中,有些代码只想从程序启动就只执行一次,典型的应用场景就是"单例" // MARK: 一次性执行 - (void)once { static dis ...

  9. JQUERY 判断选择器选择的对象 是否存在

    判断方法: 直接选择判断,是不正确的方法,因为 $(“#id”) 不管对象是否存在都会返回 object . if($("#id")){ alert('存在'); }else{ a ...

  10. hdu 1622 Trees on the level

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...