Advertisement
Guest User

Untitled

a guest
May 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int TAP = 0;
  5. const int SINK = 201;
  6. const int MAXNODE = 202;
  7. const int PREF = 101;
  8.  
  9. int G[MAXNODE][MAXNODE];
  10. int c[MAXNODE][MAXNODE];
  11. int d[MAXNODE];
  12. int p[MAXNODE];
  13. int K, A, V, M;
  14.  
  15. void read_input(void) {
  16. for (int i(0); i < MAXNODE; ++i)
  17. fill(c[i], c[i] + MAXNODE, 1e9);
  18. for (int i(1); i <= 100; ++i)
  19. {
  20. c[TAP][i] = 0;
  21. c[i][TAP] = 0;
  22. c[i+PREF][SINK] = 0;
  23. c[SINK][i+PREF] = 0;
  24. G[TAP][i] = 1;
  25. G[i+PREF][SINK] = 1;
  26. }
  27. cin >> A >> V >> M >> K;
  28.  
  29. for (int i(0); i < M; ++i)
  30. {
  31. int u, v, cost;
  32. cin >> u >> v >> cost;
  33. ++u;
  34. v = v + 1 + PREF;
  35. G[u][v]++;
  36. c[u][v] = -cost;
  37. c[v][u] = cost;
  38. }
  39. }
  40.  
  41. int bellman_ford(int t, int s)
  42. {
  43. fill(d, d + MAXNODE, 1e9);
  44. fill(p, p + MAXNODE, -1);
  45.  
  46. d[t] = 0;
  47. for (int i(0); i < MAXNODE; ++i)
  48. for (int j(0); j < MAXNODE; ++j)
  49. if (d[j] != 1e9)
  50. for (int k(0); k < MAXNODE; ++k)
  51. if (G[j][k] > 0 and d[k] > d[j] + c[j][k])
  52. {
  53. d[k] = d[j] + c[j][k];
  54. p[k] = j;
  55. }
  56. return -d[s];
  57. }
  58.  
  59. int solve(int t, int s)
  60. {
  61. int ans(0);
  62. for (int i(0); i < K; ++i) {
  63. ans += bellman_ford(t, s);
  64. int cur = s;
  65. while (cur != t) {
  66. G[p[cur]][cur]--;
  67. G[cur][p[cur]]++;
  68. cur = p[cur];
  69. }
  70. }
  71. return ans;
  72. }
  73.  
  74. int main(void)
  75. {
  76. ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  77.  
  78. read_input();
  79. cout << solve(TAP, SINK) << endl;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement