ZOJ 2314 (sgu 194) Reactor Cooling (无源汇有上下界最大流)
题意:
给定n个点和m条边, 每条边有流量上下限[b,c], 求是否存在一种流动方法使得每条边流量在范围内, 而且每个点的流入 = 流出
分析:
无源汇有上下界最大流模板, 记录每个点流的 in 和 out , 然后如果一个点 i 的in > out, 从源点i连一条边到in, out > in 就从i 连一条边到 v.
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int maxN = 1e5 + ;
const int maxM = 2e6 + ;
const int INF = 1e9 + ;
int n, m, S, T, ecnt;
int in[maxN], out[maxN], B[maxN];
int head[maxN];
struct {
int to, nxt, w;
} edge[maxM];
void init() {
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(B, , sizeof(B));
ecnt = ;
memset(head, -, sizeof(head));
}
void addEdge(int u, int v, int w) {
edge[ecnt].nxt = head[u];
edge[ecnt].to = v;
edge[ecnt].w = w;
head[u] = ecnt++;
}
int depth[maxN]; bool bfs() {
memset(depth, -, sizeof(depth));
queue<int> q;
depth[S] = ;
q.push(S);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; i != -; i = edge[i].nxt) {
int v = edge[i].to, w = edge[i].w;
if(w > && depth[v] == -) { //若该残量不为0,且V[i]还未分配深度,则给其分配深度并放入队列
depth[v] = depth[u] + ;
q.push(v);
}
}
}
if(depth[T] == -)
return false;
return true;//当汇点的深度不存在时,说明不存在分层图,同时也说明不存在增广路
}
int dfs(int u, int flow) { //u为当前节点 , flow为当前流量
if(u == T) //已经到达汇点, 直接返回
return flow; for(int i = head[u]; i != -; i = edge[i].nxt) {
int v = edge[i].to, w = edge[i].w;
if((depth[v] == depth[u] + ) && (w != )) { //注意这里要满足分层图和残量不为0两个条件
int di = dfs(v, min(flow, w));
if(di > ) {
edge[i].w -= di;
edge[i ^ ].w += di; //边是相反的两条, 奇数-1 偶数+1
return di;
}
}
}
return ; //没有増广路
}
int Dinic() {
int ans = , d = ;
while(bfs()) {
while(d = dfs(S, INF))
ans += d;
}
return ans;
}
int main() {
// freopen("1.txt","r", stdin);
ios::sync_with_stdio(false);
int Test;
cin >> Test;
while(Test--) {
cin >> n >> m;
init();
S = n + , T = n + ;
for(int i = ; i < m; i++) {
int u, v, b, c;
cin >> u >> v >> b >> c;
addEdge(u,v,c - b);
addEdge(v,u, );
B[i] = b;
out[u] += b;//记录入流
in[v] += b;// 记录出流
} int sum = ; for(int i = ; i <= n; i++) { //加边
int tmp = in[i] - out[i];
if(tmp > ) {
addEdge(S, i, tmp);
addEdge(i, S, );
sum += tmp;
} else {
addEdge(i, T, -tmp);
addEdge(T, i, );
}
} int ans = Dinic(); if(ans == sum) {
puts("YES");
for(int i = ; i < m; i ++)
printf("%d\n",B[i] + edge[i * + ].w); //输出的是下限 + 反向边
}else{
puts("NO");
}
puts("");
}
}
ZOJ 2314 (sgu 194) Reactor Cooling (无源汇有上下界最大流)的更多相关文章
- SGU 194 Reactor Cooling 无源汇带上下界可行流
Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output ...
- SGU 194. Reactor Cooling(无源汇有上下界的网络流)
时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...
- ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...
- sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] ...
- Zoj 2314 Reactor Cooling(无源汇有上下界可行流)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...
- HDU 4940 Destroy Transportation system(无源汇有上下界最大流)
看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...
- hdu 4940 无源汇有上下界最大流
/* <img src="http://img.blog.csdn.net/20140823174212937?watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)
#115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...
- 2018.08.20 loj#115. 无源汇有上下界可行流(模板)
传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...
随机推荐
- C#数据类型 值传递和引用传递
/// <summary> /// 电脑类 /// </summary> public class Computer { public string Type { get; s ...
- EF5 通用数据层 增删改查操作,泛型类
using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using Syste ...
- I/O操作总结(一)
所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节(我想,文章太长了,谁都没耐心翻到最后) 对于文件内容的操作 ...
- linux下指定特定用户执行命令
虽然很简单但是百度找的大部分不能用,我是没找到,后来从google找到的 sudo -H -u www bash -c 'nohup /home/web/ke/upfileserver /home/w ...
- SpringBoot 封装返回类以及session 添加获取
1.创建返回类Result public class Result<T>{ /*错误码*/ private Integer code; /*提示信息 */ private String m ...
- Mind must be master of the body, strong mind can separate the body from its suffering.
Mind must be master of the body, strong mind can separate the body from its suffering.意志是身体的主人,有顽强的意 ...
- SQL数据库基础三
- jQuery中slim版本与普通版本的区别
在jQuery3中,推出了一个slim版本.slim,百度翻译:细长的; 苗条的,纤细的; 微小的; 无价值的. 区别概述: slim即简化版,比普通版本缺少Ajax和特效模块模块. 官方发布地址:h ...
- 使用poi或jxl,通过java读写xls、xlsx文档
package nicetime.com.baseutil; import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffExcepti ...
- C# XML序列化/反序列化类XmlSerializer使用示例
using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; ...