题目链接: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:

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 - [无源汇上下界可行流]的更多相关文章

  1. ZOJ2314 Reactor Cooling(无源汇上下界可行流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

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

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

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

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

  4. hdu 4940 Destroy Transportation system (无源汇上下界可行流)

    Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  5. zoj2314 无源汇上下界可行流

    题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...

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

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

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

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

  8. 【有上下界的网络流】ZOJ2341 Reactor Cooling(有上下界可行流)

     Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidi ...

  9. 有源汇上下界可行流(POJ2396)

    题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...

随机推荐

  1. 【代码审计】JTBC(CMS)_PHP_v3.0 任意文件删除漏洞分析

      0x00 环境准备 JTBC(CMS)官网:http://www.jtbc.cn 网站源码版本:JTBC_CMS_PHP(3.0) 企业版 程序源码下载:http://download.jtbc. ...

  2. PHP代码审计笔记--任意文件上传

     0x01 最简单的文件上传 未进行文件类型和格式做合法性校验,任意文件上传 漏洞代码示例: 新建一个提供上传文件的 upload.html <html> <body> < ...

  3. PHP代码审计笔记--CSRF漏洞

    0x01 前言 CSRF(Cross-site request forgery)跨站请求伪造.攻击者盗用了你的身份,以你的名义向第三方网站发送恶意请求,对服务器来说这个请求是完全合法的,但是却完成了攻 ...

  4. RF实现多次失败重跑结果合并的基础方法和优化方法

    实现思路:通过分次执行失败案例重跑,然后通过结果文件合并命令实现多次失败重跑结果文件的合并,并输出合并后的log和report文件: 说明:具体失败案例重跑命令和结果文件合并命令请参考本博客其他相关章 ...

  5. C#实现新建文件并写入内容

    using System; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(stri ...

  6. 【Java并发编程二】同步容器和并发容器

    一.同步容器 在Java中,同步容器包括两个部分,一个是vector和HashTable,查看vector.HashTable的实现代码,可以看到这些容器实现线程安全的方式就是将它们的状态封装起来,并 ...

  7. “std”: 具有该名称的命名空间不存在

    当只用using namesp std 时,会报 error C2871: “std”: 具有该名称的命名空间不存在. 包含一个含有std的头文件就不会报错了,比如<iostream>.& ...

  8. hive异常:创建MySQL时Specified key was too long; max key length is 1000 bytes

    2015-11-13 14:44:44,681 ERROR [main]: DataNucleus.Datastore (Log4JLogger.java:error(115)) - An excep ...

  9. PHP中$_SERVER的详细用法

    PHP中$_SERVER的详细用法 $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关. $_SERVER['argv'] #传递给该脚本的参数. ...

  10. Git学习(一)(2015年11月12日)

    环境:win10 已安装git工具(如未配置环境变量需先配置环境变量) 如何配置环境变量:.我的电脑-属性-高级系统设置-环境变量-系统变量 找到path然后在变量值结尾增加路径: ;C:\Progr ...