Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. /*
  2. ID: dbatyr02
  3. PROG: ditch
  4. LANG: C++11
  5. */
  6. /**
  7. Template by Danel Batyrbek
  8. All rights reserved 2017 (lol)
  9. */
  10.  
  11.  
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <queue>
  16. #include <stack>
  17. #include <set>
  18. #include <map>
  19. #include <cstdio>
  20. #include <ctime>
  21. #include <cstring>
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef double ld;
  27. typedef long double lld;
  28. typedef pair <int, int> pii;
  29. typedef pair <ll, ll> pll;
  30.  
  31. const int N = 1e5 + 10;
  32. const int N3 = 1e3 + 10;
  33. const int INF = 2e9 + 10;
  34. const int mod = 1e9 + 7;
  35. const ll LINF = 4e18 + 10;
  36.  
  37. #define fr first
  38. #define sc second
  39. #define pb push_back
  40. #define mkp make_pair
  41. #define ins insert
  42. #define all(x) x.begin(),x.end()
  43. #define speed_up ios_base::sync_with_stdio(0);cin.tie(0)
  44. #define debug(x) cerr << x << '\n'
  45. #define fname "ditch"
  46.  
  47. int n, m, s, t, last[300], d[300];
  48. vector <vector <ll> > c, f, g;
  49. bool u[300];
  50.  
  51. /*struct edge {
  52. int x, to, f, c;
  53. edge () : x(), to(), f(), c() {}
  54. edge (int x, int to, int f, int c) x(x), to(to), f(f), c(c) {}
  55. }*/
  56.  
  57. bool bfs(int x){
  58. for(int i = 1; i <= n; ++ i){
  59. d[i] = INF;
  60. }
  61. d[x] = 0;
  62. queue <int> q;
  63. q.push(x);
  64. while(q.size()){
  65. int v = q.front();
  66. q.pop();
  67. for(int i = 0; i < g[v].size(); ++ i){
  68. int to = g[v][i];
  69. if(f[v][to] < c[v][to] && d[to] == INF){
  70. q.push(to);
  71. d[to] = d[v] + 1;
  72. }
  73. }
  74. }
  75. return (d[t] != INF);
  76. }
  77.  
  78. ll dfs(int x, ll minC){
  79. //cerr << x << '\n';
  80. if(x == t || minC == 0){
  81. return minC;
  82. }
  83. for(int i = last[x]; i < g[x].size(); ++ i){
  84. int to = g[x][i];
  85. //cerr << "TO: " << to << ", D[X]: " << d[x] << ", D[TO]: " << d[to] << '\n';
  86. if(d[to] == d[x] + 1){
  87. ll delta = dfs(to, min(minC, c[x][to] - f[x][to]));
  88. if(delta){
  89. f[x][to] += delta;
  90. f[to][x] -= delta;
  91. return delta;
  92. }
  93. }
  94. last[x] = i;
  95. }
  96. return 0;
  97. }
  98.  
  99. ll mxFlow(){
  100. ll mx = 0;
  101. while(bfs(s)){
  102. //for(int i = 1; i <= n; ++ i){
  103. // cerr << d[i] << ' ';
  104. //}
  105. //cerr << INF << '\n';
  106. memset(last, 0, sizeof last);
  107. memset(u, 0, sizeof u);
  108. ll flow = dfs(s, INF);
  109. while(flow != 0){
  110. mx += flow;
  111. flow = dfs(s, INF);
  112. }
  113. }
  114. return mx;
  115. }
  116.  
  117. int main(){
  118. #ifndef DEBUG
  119. freopen(fname".in", "r", stdin);
  120. freopen(fname".out", "w", stdout);
  121. #endif
  122. cin >> m >> n;
  123. g.resize(n + 1);
  124. c.resize(n + 1);
  125. f.resize(n + 1);
  126. for(int i = 0; i <= n; ++ i){
  127. c[i].resize(n + 1);
  128. f[i].resize(n + 1);
  129. }
  130. for(int i = 1; i <= m; ++ i){
  131. int x, y, z;
  132. cin >> x >> y >> z;
  133. g[x].pb(y);
  134. g[y].pb(x);
  135. c[x][y] = z;
  136. //g[i] = edge(x, y, 0, c);
  137. }
  138. s = 1, t = n;
  139. cout << mxFlow() << '\n';
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement