1. #include <cstdlib>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const int N = + ;
  11. const int oo = 0x3f3f3f3f;
  12.  
  13. struct Edge {
  14. int from, to, cap, flow;
  15. };
  16.  
  17. struct Dinic {
  18. int n, m, s, t;
  19. int dis[N], cur[N], que[N << ];
  20. bool vis[N];
  21. vector <Edge> edges;
  22. vector <int> G[N];
  23.  
  24. void add(int from, int to, int cap) {
  25. edges.push_back((Edge) {from, to, cap, });
  26. edges.push_back((Edge) {to, from, , });
  27. m = edges.size();
  28. G[from].push_back(m - );
  29. G[to].push_back(m - );
  30. }
  31.  
  32. bool bfs() {
  33. int head = , tail = ;
  34.  
  35. memset(vis, false, sizeof vis);
  36. dis[s] = ; vis[s] = true; que[head] = s;
  37. while(head <= tail) {
  38. int x = que[head];
  39.  
  40. for(int i = ; i < (signed) G[x].size(); ++ i) {
  41. Edge &e = edges[G[x][i]];
  42.  
  43. if(!vis[e.to] && e.cap > e.flow) {
  44. vis[e.to] = true;
  45. dis[e.to] = dis[x] + ;
  46. que[++ tail] = e.to;
  47. }
  48. }
  49. ++ head;
  50. }
  51. return vis[t];
  52. }
  53.  
  54. int dfs(int x, int a) {
  55. if(x == t || a == ) return a;
  56.  
  57. int flw = , f;
  58.  
  59. for(int &i = cur[x]; i < (signed) G[x].size(); ++ i) {
  60. Edge &e = edges[G[x][i]];
  61.  
  62. if(dis[e.to] == dis[x] + && (f = dfs(e.to, min(a, e.cap - e.flow))) > ) {
  63. e.flow += f; edges[G[x][i] ^ ].flow -= f; flw += f; a -= f;
  64. if(a == ) break;
  65. }
  66. }
  67. return flw;
  68. }
  69.  
  70. int MaxFlow(int s, int t) {
  71. this->s = s; this->t = t;
  72.  
  73. int flw = ;
  74.  
  75. while(bfs()) {
  76. memset(cur, , sizeof cur);
  77. flw += dfs(s, oo);
  78. }
  79. return flw;
  80. }
  81. }net;
  82.  
  83. int n, M[N];
  84.  
  85. int main() {
  86. #ifndef ONLINE_JUDGE
  87. freopen("maxflowb.in", "r", stdin);
  88. freopen("maxflowb.out", "w", stdout);
  89. #endif
  90.  
  91. int Up, Down;
  92.  
  93. scanf("%d", &n); net.n = n + ;
  94. for(int i = ; i <= n; ++ i) {
  95. for(int j = ; j <= n; ++ j) {
  96. scanf("%d%d", &Down, &Up);
  97. M[i] -= Down; M[j] += Down;
  98. net.add(i, j, Up - Down);
  99. }
  100. }
  101. net.add(n, , oo);
  102. for(int i = ; i <= n; ++ i) {
  103. if(M[i] > ) {
  104. net.add(, i, M[i]);
  105. }
  106. else if(M[i] < ){
  107. net.add(i, n + , -M[i]);
  108. }
  109. }
  110. net.MaxFlow(, n + );
  111. printf("%d\n", net.MaxFlow(, n));
  112.  
  113. #ifndef ONLINE_JUDGE
  114. fclose(stdin); fclose(stdout);
  115. #endif
  116. return ;
  117. }

Cogs 12

连边方式见图。

Cogs 12 运输问题2 (有上下界网络流)的更多相关文章

  1. Cogs 12. 运输问题2(有上下界的有源汇最大流)

    运输问题2 ★★☆ 输入文件:maxflowb.in 输出文件:maxflowb.out 简单对比 时间限制:1 s 内存限制:128 MB 运输问题 [问题描述] 一个工厂每天生产若干商品,需运输到 ...

  2. hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )

    题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)

    https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...

  4. 算法笔记--最大流和最小割 && 最小费用最大流 && 上下界网络流

    最大流: 给定指定的一个有向图,其中有两个特殊的点源S(Sources)和汇T(Sinks),每条边有指定的容量(Capacity),求满足条件的从S到T的最大流(MaxFlow). 最小割: 割是网 ...

  5. POJ 2396 Budget(有源汇上下界网络流)

    Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...

  6. HDU 4940 Destroy Transportation system(无源汇上下界网络流)

    Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...

  7. ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心或有源汇上下界网络流)

    "Oh, There is a bipartite graph.""Make it Fantastic."X wants to check whether a ...

  8. [BZOJ2502]清理雪道 有上下界网络流(最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MB Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...

  9. uoj132/BZOJ4200/洛谷P2304 [Noi2015]小园丁与老司机 【dp + 带上下界网络流】

    题目链接 uoj132 题解 真是一道大码题,,,肝了一个上午 老司机的部分是一个\(dp\),观察点是按\(y\)分层的,而且按每层点的上限来看可以使用\(O(nd)\)的\(dp\),其中\(d\ ...

  10. 【有上下界网络流】【ZOJ】2314 Reactor Cooling

    [算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...

随机推荐

  1. 层次查询start with ... connect by

    如:select distinct dep_id from t_sys_dep_dimensions start with dep_id = (select dept_id from t_sys_pe ...

  2. jquery实现简单鼠标经过图片预览效果

    html结构:<div class="prebtn"><img src=""/></div> css代码:#preview{ ...

  3. sql解释执行顺序

    一.查询的逻辑执行顺序 (1) FROM left_table (3) join_type JOIN right_table (2) ON join_condition (4) WHERE where ...

  4. WIN8 WIN10系统如何完全获取用户管理员权限

    按住WIN+R 2 计算机配置----Windows设置----安全设置----本地策略----安全选项----用户账户控制:以管理员批准模式运行所有管理员,把启用改为禁止然后重启电脑

  5. Python学习笔记整理(十一)Python的while和for循环

    while语句,提供了编写通用循环的一种方法,而for语句是用来遍历序列对象内的元素,并对每个元素运行一个代码块.break,continue用在循环内,跳出整个循环或者跳出一次循环. 一.while ...

  6. MySQL锁等待分析【1】

    场景: 昨天业务系统上遇到了数据库慢的问题(对dcsdba.og_file_audit表的insert 慢&超时).分析后定位到是由于锁等待造成的.分析过程如下: 1.执行show proce ...

  7. Chrome下的语音控制框架MyVoix.js使用篇(二)

    上一篇博文中,初步介绍了MyVoix.js的基本功能,这次我们将演示一个完整的实例. 先上代码 <!DOCTYPE HTML> <html> <head> < ...

  8. FJ省队集训DAY2 T1

    思路:转换成n条三维空间的直线,求最大的集合使得两两有交点. 有两种情况:第一种是以某2条直线为平面,这时候只要统计这个平面上有几条斜率不同的直线就可以了 还有一种是全部交于同一点,这个也只要判断就可 ...

  9. #ifdef __cplusplus extern "C" {代码} 倒底是什么意思?

    时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus   extern "C" { #endif //一段代码 #ifdef __cplusplus } # ...

  10. bzoj1148

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1148 很常见的排序贪心题...... 假设我们得到了一个最优序列,记s[n]=w[1]+w[2 ...