ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314
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
题意:
一个核反应堆的冷却系统有 $n$ 个结点,有 $m$ 条单向的管子连接它们,管子内流量有上下界限,问能否使液体在整个系统中循环流动。
题解:
根据论文《一种简易的方法求解流量有上下界的网络中网络流问题》,如果我们在网络流的基础定义上添加一下条件,就可以构造容量有上下界的网络流:
对于网络 $G=(V,E,B,C)$,对于任意一条有向边 $(u,v) \in E$,$b(u,v)$ 代表其容量下界,$c(u,v)$ 则依然代表其容量上界,
那么网络 $G=(V,E,B,C)$ 中的可行流,在依然满足反对称性、流量守恒的基础上,对容量限制则稍作添加:对于 $\forall u,v \in V$,要求 $b(u,v) \le f(u,v) \le c(u,v)$。
相应的,也可以定义最小流概念,即所有可行流中流值最小的。
本题的网络中,没有设置源汇点,但是所有点都必须满足流量守恒性质,这类问题叫做无源汇上下界可行流。
普通的最大流,仅仅要求 $f(u,v) \ge 0$,那么,不妨设一个一个新流
$g\left( {u,v} \right) = f\left( {u,v} \right) - b\left( {u,v} \right)$
这样一来,由于要求 $g\left( {u,v} \right) \ge 0$,那么实际的流就可以满足 $f(u,v) \ge b(u,v)$,
那么,容易得到新流的上界为
$c\left( {u,v} \right) - b\left( {u,v} \right)$
然而,这样一来,我们实际上构造了一个新网络 $G' = (V,E,C - B)$,它仅仅缩小了每条边的容量上界,依然体现不了下界对于流的限制,
所以我们还需要将式子 $g\left( {u,v} \right) = f\left( {u,v} \right) - b\left( {u,v} \right)$ 代入流量守恒的式子中,得到
$\sum\limits_{(u,i) \in E} {[g(u,i) + b(u,i)]} = \sum\limits_{(i,v) \in E} {[g(i,v) + b(i,v)]}$
$\sum\limits_{(i,v) \in E} {g(i,v)} - \sum\limits_{(u,i) \in E} {g(u,i)} = \sum\limits_{(u,i) \in E} {b(u,i)} - \sum\limits_{(i,v) \in E} {b(i,v)}$
此时,上式左边代表了节点的流出量减去流入量,而右边则是节点的入弧下界和减去出弧下界和,我们记等式右边为 $M(i)$,即
$M(i) = \sum\limits_{(u,i) \in E} {b(u,i)} - \sum\limits_{(i,v) \in E} {b(i,v)}$
若 $M(i) \ge 0$,则
$M(i) + \sum\limits_{(u,i) \in E} {g(u,i)} = \sum\limits_{(i,v) \in E} {g(i,v)}$
若 $M(i) < 0$,则
$\sum\limits_{(u,i) \in E} {g(u,i)} = \sum\limits_{(i,v) \in E} {g(i,v)} - M(i)$
将上面两式与普通的流量守恒式子 $\sum\limits_{(u,i) \in E} {f(u,i)} = \sum\limits_{(i,v) \in E} {f(i,v)}$ 比较发现,
对于新网络 $G' = (V,E,C - B)$ 的流,其流量守恒性质遭到破坏(或者说,要求另一种新的流量平衡),
那么,我们怎么判断这种新的流量平衡是否被满足了呢,很简单,先构建源汇点,
对于 $M(i) \ge 0$,$M(i) + \sum\limits_{(u,i) \in E} {g(u,i)} = \sum\limits_{(i,v) \in E} {g(i,v)}$ 这种情况的节点,建有向边 $(s,i)$,其容量 $c(s,i)$ 为 $M(i)$;
对于 $M(i) < 0$,$\sum\limits_{(u,i) \in E} {g(u,i)} = \sum\limits_{(i,v) \in E} {g(i,v)} - M(i)$ 这种情况的节点,建有向边 $(i,t)$,其容量 $c(i,t)$ 为 $- M(i)$;
这样,当我们对这个网络进行普通的求最大流后,若所有连接源点和汇点的弧都是满流的,那么就代表这种新的流量平衡已经满足了。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=+;
const int maxm=maxn*maxn; struct Edge{
int u,v,c,f;
};
struct Dinic
{
int s,t; //源点汇点
vector<Edge> E;
vector<int> G[maxn];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
int addedge(int from,int to,int cap)
{
E.push_back((Edge){from,to,cap,});
E.push_back((Edge){to,from,,});
G[from].push_back(E.size()-);
G[to].push_back(E.size()-);
return E.size()-;
}
int dist[maxn],vis[maxn];
queue<int> q;
bool bfs() //在残量网络上构造分层图
{
memset(vis,,sizeof(vis));
while(!q.empty()) q.pop();
q.push(s);
dist[s]=;
vis[s]=;
while(!q.empty())
{
int now=q.front(); q.pop();
for(int i=;i<G[now].size();i++)
{
Edge& e=E[G[now][i]]; int nxt=e.v;
if(!vis[nxt] && e.c>e.f)
{
dist[nxt]=dist[now]+;
q.push(nxt);
vis[nxt]=;
}
}
}
return vis[t];
}
int dfs(int now,int flow)
{
if(now==t || flow==) return flow;
int rest=flow,k;
for(int i=;rest> && i<G[now].size();i++)
{
Edge &e=E[G[now][i]]; int nxt=e.v;
if(e.c>e.f && dist[nxt]==dist[now]+)
{
k=dfs(nxt,min(rest,e.c-e.f));
if(!k) dist[nxt]=; //剪枝,去掉增广完毕的点
e.f+=k; E[G[now][i]^].f-=k;
rest-=k;
}
}
return flow-rest;
}
int mf; //存储最大流
int maxflow()
{
mf=;
int flow=;
while(bfs()) while(flow=dfs(s,INF)) mf+=flow;
return mf;
}
}dc; int n,m;
int B[maxm];
int M[maxn];
int Eidx[maxm];
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m); dc.s=,dc.t=n+;
dc.init(,n+); memset(M,,sizeof(M));
for(int u,v,b,c,i=;i<=m;i++)
{
scanf("%d%d%d%d",&u,&v,&b,&c);
Eidx[i]=dc.addedge(u,v,c-b);
B[i]=b;
M[v]+=b;
M[u]-=b;
} int tmp=;
for(int i=;i<=n;i++)
{
if(M[i]>=) dc.addedge(dc.s,i,M[i]),tmp+=M[i];
else dc.addedge(i,dc.t,-M[i]);
}
dc.maxflow(); if(dc.mf!=tmp) printf("NO\n");
else
{
printf("YES\n");
for(int i=;i<=m;i++)
{
printf("%d\n",dc.E[Eidx[i]].f+B[i]);
}
}
printf("\n");
}
}
ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]的更多相关文章
- ZOJ2314 Reactor Cooling(无源汇上下界可行流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...
- ZOJ 2314 Reactor Cooling [无源汇上下界网络流]
贴个板子 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...
- zoj 2314 Reactor Cooling (无源汇上下界可行流)
Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...
- hdu 4940 Destroy Transportation system (无源汇上下界可行流)
Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 ...
- zoj2314 无源汇上下界可行流
题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...
- ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...
- ZOJ 2314 Reactor Cooling | 无源汇可行流
题目: 无源汇可行流例题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题解: 证明什么的就算了,下面给出一种建图方式 ...
- 【有上下界的网络流】ZOJ2341 Reactor Cooling(有上下界可行流)
Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidi ...
- 有源汇上下界可行流(POJ2396)
题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...
随机推荐
- Spring踩坑记录
1. Spring properties配置项不能解析问题 本地部分配置文件迁到disconf,希望disconf的配置文件交由spring托管.这样的话,原有代码中引用配置的地方就不用变(还是用${ ...
- CSS3制作美丽的3D表单
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 在Windows系统上搭建aria2下载器
Aria2是一个命令行下运行.多协议.多来源下载工具(HTTP/HTTPS.FTP.BitTorrent.Metalink),并且支持迅雷离线以及百度云等常用网盘的多线程下载(甚至可以超过专用客户端的 ...
- Linux 日常运维
查看用户信息:w 查看系统负载:uptime 查看系统资源使用情况:vmstat 查看进程动态:top 查看网卡流量:sar 查看网卡流量:nload 查看磁盘读写:iostat 查看磁盘读写:iot ...
- PyCharm 基础设置
设置主题:File -- Settings -- Editor -- Color & Fonts -- Font -- Scheme 设置为 Darcula 设置字体:File -- Sett ...
- 安装RVDS2.2
本人经过一晚上的折腾,已经将rvds2.2成功部署在为AMD平台的CPU上面,除了些许小BUG外,编译程序无任何错误,可成功将产上的AXF文件通过Jlink烧制到开发板上. 感谢cdly7475为我们 ...
- 第二十篇:不为客户连接创建子进程的并发回射服务器(poll实现)
前言 在上文中,我使用select函数实现了不为客户连接创建子进程的并发回射服务器( 点此进入 ).但其中有个细节确实有点麻烦,那就是还得设置一个client数组用来标记select监听描述符集中被设 ...
- ScaleType属性
FIT_CENTER 把原图按照比例放大缩小到ImageView的高度,显示在ImageView的center(中部/居中显示). 1 2 CENTER_CROP 会拉伸图片以原图填满ImageV ...
- 在apache虚拟目录配置
在apache虚拟目录配置中 <VirtualHost *:80>xxx xxx xxx</VirtualHost> 不能写成 <VirtualHost *>xxx ...
- MongoDB开篇
1.安装MongoDB 官方下载地址 https://www.mongodb.com/download-center#community 这个文件下载的有些奇怪,这个zip的文件下载下来和百度出来 ...