Advertisement
Guest User

ford

a guest
Sep 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. vector< vector <int> > matrix; //пропускная способность(наличие ребра)
  8. vector<int> S;//множество вершин Si
  9.  
  10. vector<bool> used;
  11. vector<int> metka;
  12. vector<int> way;
  13.  
  14. int head;
  15. int F;
  16. int iteration=0;
  17. const int super_metka=-14567;
  18. void start1()
  19. {
  20.  
  21. used[0]=true;
  22. metka[0]=0;
  23. way[0]=-1;
  24. for(size_t i=1;i<head;i++)
  25. {
  26. used[i]=false;
  27. metka[i]=0;
  28. way[i]=-1;
  29. }
  30. }
  31.  
  32. int bfs(int start,int end) //конец не учёл!!(шаг 2,6)
  33. {
  34. queue<int> turn; //очередь с номерами вершин
  35. start1();
  36.  
  37. turn.push(start); //начало итерации, первая вершина
  38. int j=0;
  39.  
  40. while(j!=end)
  41. {
  42. int turn_tail=turn.front();
  43. //if(iteration==2)
  44. //cout<<turn.front()<<' ';
  45. turn.pop();
  46. //if(iteration==2)
  47. //cout<<turn.front();
  48. int max=0;
  49. //if(iteration==2)
  50. //cout<<turn.front()<<' '<<turn_tail<<endl;;
  51. for(size_t i=0;i<matrix[turn_tail].size();i++)
  52. {
  53. if(matrix[turn_tail][i]!=0 && !used[i] &&
  54. matrix[turn_tail][i]>max)
  55. {
  56. max=matrix[turn_tail][i];
  57. j=i;
  58. //if(iteration==2)
  59. //cout<<max<<' '<<j;
  60. }
  61. }
  62.  
  63. if(max==0 && turn_tail==0)
  64. return 0;
  65.  
  66. if(max==0 && turn_tail!=0)
  67. {//шаг 4, удаление;есть сомнения
  68.  
  69. metka[j]=super_metka;
  70. int smth=way[j];
  71. way[j]=-1;
  72. j=smth;
  73.  
  74. turn.push(j);
  75. //cout<<turn.front()<<endl;
  76. }
  77. else{
  78.  
  79. turn.push(j);
  80. used[j]=true;
  81.  
  82. metka[j]=max;
  83.  
  84. way[j]=turn_tail;
  85. //if(iteration==2){
  86. //for(int i=0;i<5;i++)
  87. // cout<<way[i]<<' ';
  88. // cout<<endl;
  89. //}
  90. }
  91. // if(iteration==2)
  92. // cout<<j<<endl;
  93. }
  94. iteration++;
  95. /*if(iteration==3)
  96. {
  97. for(int i=0;i<5;i++)
  98. cout<<metka[i]<<' ';
  99. }*/
  100. /*if(iteration==3){
  101. for(int i=0;i<5;i++)
  102. cout<<way[i]<<' ';
  103. }*/
  104. if(j!=0)
  105. return 1;
  106. return 0;
  107. }
  108.  
  109. int min()
  110. {
  111. int min=5000;
  112. /*if(iteration==3){
  113. for(size_t i=0;i<head;i++)
  114. {
  115. cout<<metka[i]<<' ';
  116. }
  117. }*/
  118. for(size_t i=0;i<head;i++)
  119. {
  120. if(metka[i] && metka[i]<min && metka[i]!=super_metka){
  121. min=metka[i];
  122.  
  123. }
  124. }
  125. //cout<<min<<endl;
  126. return min;
  127. }
  128. /*void Vivod()
  129. {
  130. cout<<endl;
  131.  
  132. for(int j=0;j<5;j++)
  133. for(int i=0;i<5;i++)
  134. {
  135. cout<<matrix[j][i]<<' ';
  136. if(i+1==5)
  137. cout<<endl;
  138. }
  139. }
  140. */
  141.  
  142. void max_flow(int start,int end)
  143. {
  144.  
  145. while(bfs(0,1)) //итерации(пока существует путь)
  146. {
  147. int fp=min();
  148. F+=fp;
  149.  
  150. for(int i=0;i<end+1;i++)
  151. {
  152. if(way[i]!=-1)
  153. {
  154.  
  155. matrix[way[i]][i]-=fp;
  156. matrix[i][way[i]]+=fp;
  157.  
  158.  
  159.  
  160. }
  161. }
  162.  
  163. }
  164. cout<<"max flow="<<F<<endl;
  165. }
  166.  
  167.  
  168. int main()
  169. {
  170. int n;
  171. freopen ("data.txt", "r", stdin);
  172. cin>>n;
  173. head=n;
  174. used.reserve(n+1);
  175. metka.reserve(n+1);
  176. way.reserve(n+1);
  177. for(size_t i=0;i<n;i++)
  178. {
  179. vector<int> temp;
  180. for(size_t j=0;j<n;j++)
  181. {
  182. int trash;
  183. cin>>trash;
  184. temp.push_back(trash);
  185. }
  186. matrix.push_back(temp);
  187. }
  188.  
  189. max_flow(0,n-1);
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement