这是模板题了吧,先建立附加源汇,然后保留每个点的in-out,如果这个值是正的,那么就从附加源先这个点连一个边权为in-out的边,否则从这个点向附加汇连一条相反数的边,剩下题目中的边就用上界-下界连就好了。

  1. #include <bits/stdc++.h>
  2. #define rep(i, a, b) for (int i = a; i <= b; i++)
  3. #define drep(i, a, b) for (int i = a; i >= b; i--)
  4. #define REP(i, a, b) for (int i = a; i < b; i++)
  5. #define mp make_pair
  6. #define pb push_back
  7. #define clr(x) memset(x, 0, sizeof(x))
  8. #define xx first
  9. #define yy second
  10. using namespace std;
  11. typedef long long i64;
  12. typedef pair<int, int> pii;
  13. const int inf = ~0U >> ;
  14. const i64 INF = ~0ULL >> ;
  15. //***********************************
  16.  
  17. const int maxn = , maxm = ;
  18.  
  19. struct Ed {
  20. int u, v, nx, c; Ed() {}
  21. Ed(int _u, int _v, int _nx, int _c) :
  22. u(_u), v(_v), nx(_nx), c(_c) {}
  23. } E[maxm << ];
  24. int G[maxn], edtot = ;
  25. void addedge(int u, int v, int c) {
  26. E[++edtot] = Ed(u, v, G[u], c);
  27. G[u] = edtot;
  28. E[++edtot] = Ed(v, u, G[v], );
  29. G[v] = edtot;
  30. }
  31.  
  32. int level[maxn], s, t;
  33. bool bfs() {
  34. static int que[maxn]; int qh(), qt();
  35. clr(level);
  36. level[que[++qt] = s] = ;
  37. while (qh != qt) {
  38. int x = que[++qh]; if (qh == maxn - ) qh = ;
  39. for (int i = G[x]; i; i = E[i].nx) if (E[i].c && !level[E[i].v]) {
  40. level[que[++qt] = E[i].v] = level[x] + ;
  41. if (qt == maxn - ) qt = ;
  42. }
  43. }
  44. return !!level[t];
  45. }
  46. int dfs(int u, int rm) {
  47. if (u == t) return rm;
  48. int rm1 = rm;
  49. for (int i = G[u]; i; i = E[i].nx) {
  50. if (E[i].c && level[E[i].v] == level[u] + ) {
  51. int flow = dfs(E[i].v, min(E[i].c, rm));
  52. E[i].c -= flow, E[i ^ ].c += flow;
  53. if ((rm -= flow) == ) break;
  54. }
  55. }
  56. if (rm1 == rm) level[u] = ;
  57. return rm1 - rm;
  58. }
  59.  
  60. int l[maxm], in[maxn], out[maxn];
  61. int cnt;
  62. int main() {
  63. clr(G), edtot = , clr(in), clr(out);
  64. int n, m;
  65. scanf("%d%d", &n, &m);
  66. s = n + , t = n + ;
  67. rep(i, , m) {
  68. int x, y, a, b; scanf("%d%d%d%d", &x, &y, &a, &b); l[i] = a;
  69. addedge(x, y, b - a);
  70. in[y] += a, out[x] += a;
  71. }
  72. int sum();
  73. rep(i, , n) {
  74. if (in[i] > out[i]) addedge(s, i, in[i] - out[i]), sum += in[i] - out[i];
  75. else addedge(i, t, out[i] - in[i]);
  76. }
  77. int ans();
  78. while (bfs()) ans += dfs(s, 0x3f3f3f3f);
  79. if (ans != sum) { puts("NO"); return ; }
  80. puts("YES");
  81. for (int i = ; i <= m; i++) printf("%d\n", E[(i << ) ^ ].c + l[i]);
  82. return ;
  83. }

sgu194 Reactor Cooling【无源汇有上下界可行流】的更多相关文章

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

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

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

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

  3. ZOJ 2314 (sgu 194) Reactor Cooling (无源汇有上下界最大流)

    题意: 给定n个点和m条边, 每条边有流量上下限[b,c], 求是否存在一种流动方法使得每条边流量在范围内, 而且每个点的流入 = 流出 分析: 无源汇有上下界最大流模板, 记录每个点流的 in 和 ...

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

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

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

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

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

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

  7. [loj#115] 无源汇有上下界可行流 网络流

    #115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据   题 ...

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

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

  9. 【LOJ115】无源汇有上下界可行流(模板题)

    点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...

  10. LibreOJ #115. 无源汇有上下界可行流

    二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...

随机推荐

  1. linux进程、管道和重定向

    1.shell先后使用fork和exec系统调用来执行一个外部命令. 2.在linux系统中,有三个文件会被内核自动打开,分别是stdin.stdout.stderr. 3.进程的属性相关命令: 查看 ...

  2. Python -- OOP高级 -- 元类

    type()函数既可以返回一个对象的类型,又可以创建出新的类型 def fn(self, name="world"): print("Hello, %s!" % ...

  3. UVA 10200 Prime Time (打表)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. discuz!迁移指南

    转自:http://jingyan.baidu.com/article/f7ff0bfc77114b2e26bb1390.html 曾经在本地搭建过一个discuz!论坛,现在买了域名和服务器,那么怎 ...

  5. linux找回密码

    开机启动系统,在进入系统之前按键盘上面的Esc键,会进入下面的界面 按键盘上的e键,出现下面界面 用键盘上的方向键移动光标到第二项,然后再按键盘上面的e,会出现下面的界面 输入一个空格键,然后在输入数 ...

  6. XAMPP命令之LAMPP

    .wiz-todo, .wiz-todo-img {width: 16px; height: 16px; cursor: default; padding: 0 10px 0 2px; vertica ...

  7. 剑指offer 二叉搜索树后续遍历序列 判断

    最后一个元素是 根节点. 左子树的元素都小于根节点,右子树都大于根节点 然后递归判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  8. ural1772 Ski-Trails for Robots

    Ski-Trails for Robots Time limit: 1.0 secondMemory limit: 64 MB One of the stages of the Robot Cross ...

  9. 关于js的一些基本知识(类,闭包,变量)

    这里写的都是些杂知识,包括私有,类,闭包这些js不可避免的东西,感觉自己有可能会误人子弟.所以有觉得写错了的读者,希望可以及时评论告诉我.我可以及时更正.多谢大家了 1.关于类的创建 类的创建大致可以 ...

  10. CALayer 进阶

    转载自:http://www.cofcool.net/development/2015/06/19/ios-study-note-eight-CALayer-info/ The CALayer cla ...