Guest User

Untitled

a guest
Jul 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <cstdio>
  2. #include <memory>
  3. #include <deque>
  4. #include <limits>
  5. #include <fstream>
  6. using namespace std;
  7. const int MAX = 500;
  8.  
  9. int source,dest,n;
  10. int counter=0;
  11.  
  12. deque<int> q;
  13. int answer[MAX];
  14. int m[MAX][MAX];
  15. int used[MAX],p[MAX];
  16. int color[MAX];
  17.  
  18. int min(int i, int j)
  19. {
  20. return (i < j) ? i : j;
  21. }
  22.  
  23.  
  24. void bfs(int v)
  25. {
  26. int i,First;
  27. q.push_front(v);
  28. while(!q.empty())
  29. {
  30. First = q[0];
  31. if (First == dest) return;
  32. q.pop_front();
  33. used[First] = 1;
  34. for(i=0;i<n;i++)
  35. if ((m[First][i] > 0) && (!used[i]))
  36. {
  37. p[i] = First;
  38. q.push_back(i);
  39. }
  40. }
  41. }
  42.  
  43.  
  44.  
  45. void Rebuild(int k, int flow)
  46. {
  47. if (p[k] == -1) return;
  48. Rebuild(p[k],flow);
  49. m[p[k]][k] -= flow;
  50. m[k][p[k]] += flow;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. int FindFlow(int k)
  58. {
  59. int x;
  60. if (p[k] == -1) return INT_MAX;
  61. x = FindFlow(p[k]);
  62. return min(x,m[p[k]][k]);
  63. }
  64. void DFSVISIT(int a) {
  65. color[a]=1;
  66.  
  67. for (int i=0; i<n; i++){
  68. if (m[a][i]!=0) {
  69.  
  70.  
  71. if (color[i]==0) {
  72. // pr[ii]=a;
  73. DFSVISIT(i);
  74. }
  75. }
  76. }
  77. color[a]=2;
  78.  
  79.  
  80. answer[counter]=a;
  81. counter++;
  82. }
  83.  
  84.  
  85. void main(void)
  86. {
  87. int a,b,c;
  88. int M;
  89. int MaxFlow,flow;
  90. std::ifstream in("cut.in");
  91. std::ofstream out("cut.out");
  92.  
  93. memset(m,0,sizeof(m));
  94. in>>n>>M;
  95. source=0;
  96. dest=n-1;
  97. MaxFlow = 0;
  98. for (int i=0; i<M; ++i)
  99. {
  100.  
  101. in >>a>>b>>c;
  102. m[a-1][b-1] = c;
  103. }
  104.  
  105.  
  106.  
  107. while (1)
  108. {
  109. q.clear();
  110. memset(p,-1,sizeof(p));
  111. memset(used,0,sizeof(used));
  112. memset(color,0,sizeof(color));
  113. bfs(source);
  114. flow = FindFlow(dest); if (flow == INT_MAX) break;
  115. MaxFlow += flow;
  116. Rebuild(dest,flow);
  117. }
  118. /*
  119.  
  120.  
  121. for (int i=0; i<n;i++) {
  122. for (int j=0;j<n;j++) {
  123.  
  124. out<< m[i][j]<< " ";
  125. }
  126. out<<"\n";
  127.  
  128. }*/
  129. DFSVISIT (0);
  130. out<<counter<<"\n";
  131. for (int i=0; i<counter-1;i++){ out << answer[i]+1<< " ";}
  132. out << answer[counter-1]+1;
  133.  
  134.  
  135.  
  136.  
  137. }
Add Comment
Please, Sign In to add comment