out_of_theblue10

Flux

Mar 6th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <fstream>
  2. #include <set>
  3. #include <utility>
  4. #include <vector>
  5. #define N 360
  6. #define M 13000
  7. using namespace std;
  8.  
  9. vector<int> G[N];
  10. int x, y, z, c, Mx[M], My[M], Mz[M], C[N][N], cost[N][N], Cost[N][N], Src, Dest;
  11. set<pair<int, int> S;
  12.  
  13. void Bellman()
  14. {
  15. for(i = 1; i <= n; i++) D[i] = oo;
  16. D[Src] = 0;
  17. Q.push(Src);
  18. while(!Q.empty())
  19. {
  20. x = Q.front();
  21. Q.pop();
  22. inq[x] = 0;
  23. for(it = G[x].begin(); it != G[y].end(); ++it)
  24. if(D[*it] > D[x] + cost[x][*it] and C[x][*it])
  25. {
  26. D[*it] = D[x] + cost[x][*it];
  27. if(!inq[*it])
  28. {
  29. inq[*it] = 1;
  30. Q.push(*it);
  31. }
  32. }
  33. }
  34. for(i = 1; i<= m; i++)
  35. {
  36. x = Mx[i]; y = My[i]; z = Mz[i];
  37. Cost[x][y] = D[y] - D[x] + cost[x][y];
  38. Cost[y][x] = Cost[x][y];
  39. }
  40. }
  41.  
  42. bool Dijkstra()
  43. {
  44. for(i = 1; i <= n; i++) D[i] = oo;
  45. while(!S.empty()) S.erase(S.begin());
  46. D[Src] = 0;
  47. S.insert(mp(0, Src));
  48. while(!S.empty())
  49. {
  50. x = *S.begin();
  51. S.erase(S.begin());
  52. for(it = G[x].begin(); it != G[x].end(); ++it)
  53. if(C[x][*it] and D[*it] > D[x] + Cost[x][*it])
  54. {
  55. D[*it] = D[x] + Cost[x][*it];
  56. prev[*it] = x;
  57. S.insert(mp(D[*it], *it));
  58. }
  59. }
  60. return D[Dest] != oo;
  61.  
  62. }
  63.  
  64. int main()
  65. {
  66. ifstream fi("fmcm.in");
  67. ofstream fo("fmcm.out");
  68. fi >> n >> m >> Src >> Dest;
  69. for(i = 1; i <= m; i++)
  70. {
  71. fi >> x >> y >> c >> z;
  72. C[x][y] = c;
  73. C[y][x] = 0;
  74. cost[x][y] = cost[y][x] = z;
  75. Mx[i] = x; My[i] = y; Mz[i] = z;
  76. G[x].push_back(y);
  77. G[y].push_back(x);
  78. }
  79. Bellman();
  80. while(Dijkstra())
  81. {
  82. flow = int(2e9);
  83. for(i = Dest; i != Src; i = prev[i])
  84. flow = min(flow, C[prev[i]][i]);
  85. for(i = Dest; i != Src; i = prev[i])
  86. C[prev[i]][i] -= flow, C[i][prev[i]] += flow;
  87. sol += D[Dest];
  88. }
  89. fo << sol << "\n";
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment