题目描述

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.

输入

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.

输出

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.

样例输入

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

样例输出

NO

YES
1
2
3
2
1
1


题目大意

给你一张n个点和m条边的图,每条边有[li,ri]的容量,求是否有可行流?有则输出一组方案。

题解

有上下界网络流无源汇可行流模板题,题意都很直白。

转化为最大流。

假设有一条容量为[l,r]的路径连通x->y,那么进行如下操作:

1.记录路径的l(求总流量时会用到)

2.加入x->y,容量为r-l的边

3.将x的流入总数in[x]减去l,将y的流入总数in[y]加上l。

处理完所有路径后,再建立超级源点和超级汇点,并扫一遍每个点。

对于点x,如果in[x]>0,则加S->x,容量为in[x]的边,否则加x->T,容量为-in[x]的边。

跑一遍最大流,如果满流则有解,否则无解。

有解时,对于每条通道i,它的总流量为下界low[i]加上新图的流出量val[i<<1|1]。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
queue<int> q;
int head[210] , to[100000] , val[100000] , next[100000] , cnt , low[100000] , in[210] , dis[100000] , s , t;
void add(int x , int y , int z)
{to[++cnt] = y;val[cnt] = z;next[cnt] = head[x];head[x] = cnt;
}
bool bfs()
{int x , i;while(!q.empty()) q.pop();memset(dis , 0 , sizeof(dis));dis[s] = 1;q.push(s);while(!q.empty()){x = q.front();q.pop();for(i = head[x] ; i ; i = next[i]){if(val[i] && !dis[to[i]]){dis[to[i]] = dis[x] + 1;if(to[i] == t) return 1;q.push(to[i]);}}}return 0;
}
int dinic(int x , int l)
{if(x == t) return l;int temp = l , k , i;for(i = head[x] ; i ; i = next[i]){if(val[i] && dis[to[i]] == dis[x] + 1){k = dinic(to[i] , min(temp , val[i]));if(!k) dis[to[i]] = 0;val[i] -= k , val[i ^ 1] += k;if(!(temp -= k)) break;}}return l - temp;
}
int main()
{int T;scanf("%d" , &T);while(T -- ){int n , m , i , x , y , z , sum = 0 , maxflow = 0;scanf("%d%d" , &n , &m);memset(head , 0 , sizeof(head));memset(in , 0 , sizeof(in));cnt = 1;s = 0 , t = n + 1;for(i = 1 ; i <= m ; i ++ ){scanf("%d%d%d%d" , &x , &y , &low[i] , &z);in[x] -= low[i] , in[y] += low[i];add(x , y , z - low[i]) , add(y , x , 0);}for(i = 1 ; i <= n ; i ++ ){if(in[i] > 0) sum += in[i] , add(s , i , in[i]) , add(i , s , 0);else if(in[i] < 0) add(i , t , -in[i]) , add(t , i , 0);}while(bfs()) maxflow += dinic(s , 0x7fffffff);if(maxflow != sum) printf("NO\n");else{printf("YES\n");for(i = 1 ; i <= m ; i ++ )printf("%d\n" , val[i << 1 | 1] + low[i]);}printf("\n");}return 0;
}

转载于:https://www.cnblogs.com/GXZlegend/p/6516651.html

【zoj2314】Reactor Cooling 有上下界可行流相关推荐

  1. 无源汇有上下界可行流(网络流进阶)

    无源汇有上下界可行流(也就是循环流) 模型:一个网络,求出一个流,使得每条边的流量必须>=Li且<=Hi, 每个点必须满足总流入量=总流出量(流量守恒)(这个流的特点是循环往复,无始无终) ...

  2. loj#115. 无源汇有上下界可行流

    \(\color{#0066ff}{ 题目描述 }\) 这是一道模板题. \(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \ ...

  3. 【bzoj2406】矩阵 二分+有上下界可行流

    题目描述 输入 第一行两个数n.m,表示矩阵的大小. 接下来n行,每行m列,描述矩阵A. 最后一行两个数L,R. 输出 第一行,输出最小的答案: 样例输入 2 2 0 1 2 1 0 1 样例输出 1 ...

  4. BZOJ 2406 LuoguP4194 矩阵 有上下界可行流

    分析: 这道题乍一看--卧槽这都什么玩意-- 然后发现给了个A矩阵,要求一个可行的B矩阵,使得矩阵C=A-B的每一行的和的绝对值和每一列的和的绝对值的最大值最小-- 好拗口啊-- 什么最大值最小之类的 ...

  5. 【ACWing】2188. 无源汇上下界可行流

    题目地址: https://www.acwing.com/problem/content/2190/ 给定一个包含nnn个点mmm条边的有向图,每条边都有一个流量下界和流量上界.求一种可行方案使得在所 ...

  6. 一篇网络流 基本模型超全总结(最大流 费用流 多源汇最大流 上下界可行流) 思路+代码模板

    文章目录 一.网络流与最大流 二.网络流三个基本性质 三.重要定义定理 四.最大流算法 <Ⅰ> Edmonds-Karp算法(EK算法) 1.EK算法 2.算法思想: 3.代码模板 4.模 ...

  7. 网络流__4 上下界可行流

    网络流 __4 上下界可行流 对于容量有上下界规范的网络流问题 无源汇上下界可行流 n个点m条边的有向图,每条边有一个流量下界和流量上界规范,求是否存在一个可行流 设原网络为(G,F),变换后网络为( ...

  8. bzoj 2406 矩阵——有源汇上下界可行流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2406 二分答案.把 b 的 n 个行作为一排, m 个列作为一排,每行和每列之间连上下界为 ...

  9. 【有源汇点上下界最小流】[SGU176]Flow construction

    题目大意 给出N个点,M条有向边 如果有向边的标号是1的话,就表示该边的上界下界都为容量 如果有向边的标号为0的哈,表示该边的下界为0,上界为容量 现在问,从1到N的最小流是多少,并输出每条边的流量 ...

最新文章

  1. 小程序工程化实践(上篇)-- 手把手教你撸一个小程序 webpack 插件,一个例子带你熟悉 webpack 工作流程...
  2. java Scanner具有神奇的作用可惜大部分java开发人员不知
  3. 1-3_基本概念_程序进程
  4. 山西出台法规规范社会力量认养文物 系全国首例
  5. Java对【JSON数据的解析】--官方解析法
  6. ubuntu下查询SSH状态和安装SSH服务
  7. 一个小码农对嵌入式的理解
  8. Android NDK--自己编写调用JNI
  9. Eclipse和Intel idea的常用技巧
  10. AngularJS API之toJson 对象转为JSON
  11. paip.为什么软件体积越来越大
  12. Java 输入输出流 解决中文乱码问题【不一定详细但一定实用篇】【全文4800字】
  13. 我的Ubuntu软件清单
  14. 解决IE8 iframe透明的问题
  15. digispark使用
  16. 工程思维:把每件事都当作一个项目来推进
  17. 微信保存图片提示失败_微信为什么保存不了图片
  18. 安卓手机连接不上电脑解决方法总结
  19. python通过ssh链接sql(python通过阿里跳板机链接阿里数据库)
  20. python html做界面_如何用EEL(python库)打开一个新的HTML页面?

热门文章

  1. node.js学习-整理
  2. Centos挂载新硬盘开机自动挂载
  3. C++---继承总结
  4. 【Express】—get根据不同的参数返回不同的数据
  5. grpc通信原理_gRPC原理简析
  6. K BEST(最大化平均值)
  7. 赚钱的方法地推拉新一定算一个
  8. 工资7500但没社保公积金,和工资4500但福利很好,这两份工作怎么选择?
  9. 为什么我的U盘传到一半速度会变成0然后过一会儿才回继续
  10. 每天固定往一个银行卡存入100元,5年之后会有多大变化?有人能坚持吗?