题意:

给定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 (无源汇有上下界最大流)的更多相关文章

  1. SGU 194 Reactor Cooling 无源汇带上下界可行流

    Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output ...

  2. SGU 194. Reactor Cooling(无源汇有上下界的网络流)

    时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...

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

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

  4. sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] ...

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

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

  6. HDU 4940 Destroy Transportation system(无源汇有上下界最大流)

    看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...

  7. hdu 4940 无源汇有上下界最大流

    /* <img src="http://img.blog.csdn.net/20140823174212937?watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  8. LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)

    #115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...

  9. 2018.08.20 loj#115. 无源汇有上下界可行流(模板)

    传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...

随机推荐

  1. NET Core项目部署

    NET Core项目部署到linux(Centos7) 阅读目录 1.开篇说明 2.Jexus简单说明 3.Visual Studio 2015本地发布并且测试 4.配置Jexus并且部署.NET C ...

  2. Entitas实现简析

    Entitas实现简析   这里主要讲Entitas的执行原理,不讲Entitas的代码生成方面. ECS简介   ECS(实体-组件-系统)是一种常用于游戏开发的架构模式.   实体: 实体只是一个 ...

  3. shell 经典

    使用新写法 这里的新写法不是指有多厉害,而是指我们可能更希望使用较新引入的一些语法,更多是偏向代码风格的,比如 尽量使用func(){}来定义函数,而不是func{} 尽量使用[[]]来代替[] 尽量 ...

  4. 055 Jump Game 跳跃游戏

    给定一个非负整数数组,您最初位于数组的第一个索引处.数组中的每个元素表示您在该位置的最大跳跃长度.确定是否能够到达最后一个索引.示例:A = [2,3,1,1,4],返回 true.A = [3,2, ...

  5. CentOS 6.2安装配置LAMP服务器(Apache+PHP5+MySQL)

    准备篇:   1.配置防火墙,开启80端口.3306端口    vi /etc/sysconfig/iptables    -A INPUT -m state --state NEW -m tcp - ...

  6. P4869 罪犯分组

    思路: 明显的dp,虽然我想到了二进制模拟,想到了转移,但还是先看了题解,原来真是这样,,,,不是第三题吗? 用f[i]表示,对于前i个罪犯最少需要分几组. 对于每个状态用二进制表示,第i位上1,0表 ...

  7. Myeclipse连接数据库删除数据库(JDBC)

    package com.test.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...

  8. IDEA创建maven项目的web.xml头

    使用IDEA创建maven项目骨架是webapp时,软件自动创建的web.xml文件是2.3版本的,不能使用el表达式,所以可以手动换成4.0的文件头. <?xml version=" ...

  9. FlowVisor相关

    1. FlowVisor工作原理(转) 作为一个网络虚拟化平台,FlowVisor部署在标准OpenFlow控制器和OpenFlow交换机之间,成为二者的透明代理.FlowVisor能够与多个控制器连 ...

  10. Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )

    图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...