题意:无源无汇有上下界的可行流 模型

思路:首先将所有边的容量设为上界减去下界,然后对一个点i,设i的所有入边的下界和为to[i],所有出边的下界和为from[i],令它们的差为dif[i]=to[i]-from[i],根据流量平衡原理,让出边和入边的下界相抵消,如果dif[i]>0,说明入边把出边的下界抵消了,还剩下dif[i]的流量必须要流过来(否则不满足入边的下界条件),这时从源点向i连一条容量为dif[i]的边来表示即可,如果dif[i]<0,同理应该从i向汇点连一条容量为-dif[i]的边。最后对新建好的图跑一遍最大流,如果源点的所有出边都满流了说明原图有可行流,可行解为每条边在新图的流量加上它的下界。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  1. #pragma comment(linker, "/STACK:10240000")
  2. #include <map>
  3. #include <set>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <deque>
  7. #include <queue>
  8. #include <stack>
  9. #include <vector>
  10. #include <cstdio>
  11. #include <string>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16.  
  17. using namespace std;
  18.  
  19. #define X first
  20. #define Y second
  21. #define pb push_back
  22. #define mp make_pair
  23. #define all(a) (a).begin(), (a).end()
  24. #define fillchar(a, x) memset(a, x, sizeof(a))
  25. #define fillarray(a, b) memcpy(a, b, sizeof(a))
  26.  
  27. typedef long long ll;
  28. typedef pair<int, int> pii;
  29. typedef unsigned long long ull;
  30.  
  31. #ifndef ONLINE_JUDGE
  32. namespace Debug {
  33. void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
  34. void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
  35. void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
  36. while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
  37. void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
  38. void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
  39. void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
  40. }
  41. #endif // ONLINE_JUDGE
  42.  
  43. template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
  44. template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
  45.  
  46. const double PI = acos(-1.0);
  47. const int INF = 0x3f3f3f3f;
  48. const double EPS = 1e-14;
  49.  
  50. /* -------------------------------------------------------------------------------- */
  51.  
  52. const int maxn = 2e2 + ;
  53.  
  54. struct Dinic {
  55. private:
  56. //const static int maxn = 1e3 + 7;
  57. struct Edge {
  58. int from, to, cap, least;
  59. Edge(int u, int v, int w, int l): from(u), to(v), cap(w), least(l) {}
  60. };
  61. int s, t;
  62. vector<Edge> edges;
  63. vector<int> G[maxn];
  64. bool vis[maxn];
  65. int d[maxn], cur[maxn];
  66.  
  67. bool bfs() {
  68. memset(vis, , sizeof(vis));
  69. queue<int> Q;
  70. Q.push(s);
  71. d[s] = ;
  72. vis[s] = true;
  73. while (!Q.empty()) {
  74. int x = Q.front(); Q.pop();
  75. for (int i = ; i < G[x].size(); i ++) {
  76. Edge &e = edges[G[x][i]];
  77. if (!vis[e.to] && e.cap) {
  78. vis[e.to] = true;
  79. d[e.to] = d[x] + ;
  80. Q.push(e.to);
  81. }
  82. }
  83. }
  84. return vis[t];
  85. }
  86. int dfs(int x, int a) {
  87. if (x == t || a == ) return a;
  88. int flow = , f;
  89. for (int &i = cur[x]; i < G[x].size(); i ++) {
  90. Edge &e = edges[G[x][i]];
  91. if (d[x] + == d[e.to] && (f = dfs(e.to, min(a, e.cap))) > ) {
  92. e.cap -= f;
  93. edges[G[x][i] ^ ].cap += f;
  94. flow += f;
  95. a -= f;
  96. if (a == ) break;
  97. }
  98. }
  99. return flow;
  100. }
  101.  
  102. public:
  103. void clear() {
  104. for (int i = ; i < maxn; i ++) G[i].clear();
  105. edges.clear();
  106. memset(d, , sizeof(d));
  107. }
  108. void add(int from, int to, int cap, int least) {
  109. edges.push_back(Edge(from, to, cap, least));
  110. edges.push_back(Edge(to, from, , least));
  111. int m = edges.size();
  112. G[from].push_back(m - );
  113. G[to].push_back(m - );
  114. }
  115.  
  116. int solve(int s, int t) {
  117. this->s = s; this->t = t;
  118. int flow = ;
  119. while (bfs()) {
  120. memset(cur, , sizeof(cur));
  121. flow += dfs(s, 1e9);
  122. }
  123. return flow;
  124. }
  125.  
  126. void out(int m) {
  127. for (int i = ; i < m; i ++) {
  128. printf("%d\n", edges[i << ].least + edges[i << | ].cap);
  129. }
  130. }
  131. };
  132. Dinic solver;
  133. int tob[maxn], fromb[maxn];
  134.  
  135. int main() {
  136. #ifndef ONLINE_JUDGE
  137. freopen("in.txt", "r", stdin);
  138. //freopen("out.txt", "w", stdout);
  139. #endif // ONLINE_JUDGE
  140. int n, m;
  141. while (cin >> n >> m) {
  142. solver.clear();
  143. fillchar(tob, );
  144. fillchar(fromb, );
  145. for (int i = ; i < m; i ++) {
  146. int u, v, b, c;
  147. scanf("%d%d%d%d", &u, &v, &b, &c);
  148. solver.add(u, v, c - b, b);
  149. tob[v] += b;
  150. fromb[u] += b;
  151. }
  152. int total = ;
  153. for (int i = ; i <= n; i ++) {
  154. int dif = tob[i] - fromb[i];
  155. if (dif > ) solver.add(, i, dif, );
  156. if (dif < ) solver.add(i, n + , - dif, );
  157. total += abs(dif);
  158. }
  159. if (solver.solve(, n + ) != total / ) puts("NO");
  160. else {
  161. puts("YES");
  162. solver.out(m);
  163. }
  164. }
  165. return ;
  166. }

[ACdream 1211 Reactor Cooling]无源无汇有上下界的可行流的更多相关文章

  1. SGU 194 Reactor Cooling Dinic求解 无源无汇有上下界的可行流

    题目链接 题意:有向图中有n(1 <= n <= 200)个点,无自环或者环的节点个数至少为3.给定每条边的最小流量和最大流量,问每条边的可行流量为多少? 思路:一般求解的网络流并不考虑下 ...

  2. 【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)

    Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s repr ...

  3. zoj3229 Shoot the Bullet(有源汇有上下界的最大流)

    题意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给给定的C个女神拍照,每天拍照数不能超过D张,而且给每个女神i拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能少于Gi,如果有解求屌 ...

  4. zoj 3229 有源汇有上下界的最大流模板题

    /*坑啊,pe的程序在zoj上原来是wa. 题目大意:一个屌丝给m个女神拍照.计划拍照n天,每一天屌丝最多个C个女神拍照,每天拍照数不能超过D张,并且给每一个女神i拍照有数量限制[Li,Ri], 对于 ...

  5. acdream 1211 Reactor Cooling 【边界网络流量 + 输出流量】

    称号:acdream 1211 Reactor Cooling 分类:无汇的有上下界网络流. 题意: 给n个点.及m根pipe,每根pipe用来流躺液体的.单向的.每时每刻每根pipe流进来的物质要等 ...

  6. Shoot the Bullet ZOJ - 3229 有源汇有上下界的最大流

    /** zoj提交评判不了,所以不知道代码正不正确.思路是应该没问题的.如果有不对的地方,请多指教. 题目:Shoot the Bullet ZOJ - 3229 链接:https://vjudge. ...

  7. BZOJ2055 80人环游世界 网络流 费用流 有源汇有上下界的费用流

    https://darkbzoj.cf/problem/2055 https://blog.csdn.net/Clove_unique/article/details/54864211 ←对有上下界费 ...

  8. sgu 194 无源汇有上下界的最大流(最大流模板dinic加优化)

    模板类型的题具体参考国家集训队论文:http://wenku.baidu.com/view/0f3b691c59eef8c75fbfb35c.html 参考博客:http://blog.csdn.ne ...

  9. bzoj 2406 二分+有源有汇上下界网络流可行流判定

    弱爆了,典型的行列建模方式,居然想不到,题做少了,总结少了...... 二分答案mid s----------------------->i行-----------------------> ...

随机推荐

  1. 远程登录redis

    没想到吧,我居然已经摸到了redis. 远程登录redis redis-cli -h 127.0.0.1 -p 6379 ip地址和端口记得换成自己的

  2. Mysql表的对应关系

    表关系 一对一一张表中的一条记录与另一张表中最多有一条明确的关系:通常,此设计方案保证两张表中使用同样的主键即可假设一张学生表:id 姓名 年龄 性别 籍贯 婚否 住址那么姓名 年龄 性别 这种字段比 ...

  3. TensorFlow keras vgg16net的使用

    from tensorflow.python.keras.applications.vgg16 import VGG16,preprocess_input,decode_predictions fro ...

  4. 单线程下实现IO切换

    1.Greenlet greenlet可以实现两个任务之间的来回切换,但遇到IO会阻塞,不会切(使用这个模块之前需要在电脑命令提示符中输入 pip3 install greenlet 进行安装) 例如 ...

  5. 15分钟从零开始搭建支持10w+用户的生产环境(三)

    上一篇文章介绍了这个架构中,选择MongoDB做为数据库的原因,及相关的安装操作. 原文地址:15分钟从零开始搭建支持10w+用户的生产环境(二)   三.WebServer 在SOA和gRPC大行其 ...

  6. BUAA_OO 第二单元总结

    作业分析 第一次作业 本次作业是单次可捎带电梯的设计,主要是初步了解多线程的设计实现和测试,本身算法设计非常简单.这次作业整体来说不是很难,是多线程的入门,主要目的就是让我们认识,了解一下什么是多线程 ...

  7. BATJ高级Java面试题分享:JVM+Redis+Kafka +数据库+设计模式

    话不多说,直接上面试题,来看一下你还欠缺多少? Mysql 与 Oracle 相比, Mysql 有什么优势? 简洁描述 Mysql 中 InnoDB 支持的四种事务隔离级别名称,以及逐级之间的区别? ...

  8. java学习(第一篇)

    Java 简介 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式 ...

  9. 写给Java程序员的Java虚拟机学习指南

    大家好,我是极客时间<深入拆解Java虚拟机>作者.Oracle Labs高级研究员郑雨迪.有幸借这个专题的机会,能和大家分享为何Java工程师要学Java虚拟机?如何掌握Java虚拟机? ...

  10. 爱创课堂每日一题第五十四天- 列举IE 与其他浏览器不一样的特性?

    IE支持currentStyle,FIrefox使用getComputStyle IE 使用innerText,Firefox使用textContent 滤镜方面:IE:filter:alpha(op ...