Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #define N 200000
  2. #define M 200000
  3. #define INF 1000000000
  4. #include <iostream>
  5. #include <cstring>
  6. #include <vector>
  7. #include <queue>
  8.  
  9. using namespace std;
  10. struct edge {
  11. int from, to, c, f;
  12. };
  13. long long flow;
  14. int n, m, s, t, d[N], ptr[N], q[N];
  15. vector<edge> e;
  16. vector<int> g[N];
  17.  
  18. void add (int a, int b, int cost) {
  19. edge e1 = { a, b, cost, 0 };
  20. edge e2 = { b, a, 0, 0 };
  21. g[a].push_back ((int) e.size());
  22. e.push_back (e1);
  23. g[b].push_back ((int) e.size());
  24. e.push_back (e2);
  25. }
  26.  
  27. bool bfs() {
  28. int qh = 0, qt = 0;
  29. q[qt++] = s;
  30. memset (d, -1, n * sizeof d[0]);
  31. d[s] = 0;
  32. while (qh < qt && d[t] == -1) {
  33. int v = q[qh++];
  34. int len = g[v].size();
  35. for (size_t i=0; i < len; ++i) {
  36. int id = g[v][i],
  37. ed = e[id].to;
  38. if (d[ed] == -1 && e[id].f < e[id].c) {
  39. q[qt++] = ed;
  40. d[ed] = d[v] + 1;
  41. }
  42. }
  43. }
  44. return d[t] != -1;
  45. }
  46.  
  47. int dfs (int v, int F) {
  48. if (!F) return 0;
  49. if (v == t) return F;
  50. int len = g[v].size();
  51. for (; ptr[v] < len; ++ptr[v]) {
  52. int id = g[v][ptr[v]],
  53. ed = e[id].to;
  54. if (d[ed] != d[v] + 1) continue;
  55. int count = dfs (ed, min (F, e[id].c - e[id].f));
  56. if (count) {
  57. e[id].f += count;
  58. e[id ^ 1].f -= count;
  59. return count;
  60. }
  61. }
  62. return 0;
  63. }
  64.  
  65. int main() {
  66. // freopen ("in.txt", "r", stdin);
  67. // freopen ("out.txt", "w", stdout);
  68.  
  69. cin >> n >> m;
  70. s = 0;
  71. t = n - 1;
  72. for (int i = 0; i < m; ++i){
  73. int a, b, cost;
  74. cin >> a >> b >> cost;
  75. --a, --b;
  76. add(a, b, cost);
  77. }
  78. int f = 0;
  79. while (1) {
  80. if (!bfs()) break;
  81. memset (ptr, 0, n * sizeof ptr[0]);
  82. while (int count = dfs (s, INF))
  83. flow += count;
  84. }
  85. cout << flow << endl;
  86. for (int i = 0; i < m; ++i){
  87. cout << e[i * 2].f << endl;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement