Advertisement
kaa7

Untitled

May 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAXN 1005
  3. #define MAXM 5005
  4. #define INF 0x3f3f3f3f
  5. using namespace std;
  6. int n, m, C[MAXN][MAXN], parent[MAXN];
  7. int viz[MAXN];
  8. vector<int> G[MAXN];
  9.  
  10. int bfs()
  11. {
  12.  
  13. memset(viz, 0, (n + 1) * sizeof(int));
  14. memset(parent, 0, (n + 1) * sizeof(int));
  15. queue<int> q;
  16. viz[1] = 1;
  17. q.push(1);
  18.  
  19. while(!q.empty()) {
  20. int nod = q.front();
  21. q.pop();
  22. if(nod == n)
  23. continue;
  24. for(int i : G[nod]) {
  25. if(!viz[i] && C[nod][i] > 0) {
  26. viz[i] = 1;
  27. parent[i] = nod;
  28. q.push(i);
  29. }
  30. }
  31. }
  32. return viz[n];
  33. }
  34.  
  35. int main()
  36. {
  37. ifstream in("maxflow.in");
  38. ofstream out("maxflow.out");
  39. in >> n >> m;
  40. vector<int> vec_in;
  41. while(m--) {
  42. int x, y, z;
  43. in >> x >> y >> z;
  44. C[x][y] = z;
  45. G[x].push_back(y);
  46. G[y].push_back(x);
  47. }
  48. int tflow = 0;
  49.  
  50. while(bfs()) {
  51. for(int j: G[n]) {
  52. if(C[j][n] > 0 && viz[j]) {
  53. parent[n] = j;
  54. int flow = INF;
  55. for(int i = n; i != 1; i = parent[i]) {
  56. if(C[parent[i]][i] < flow) {
  57. flow = C[parent[i]][i];
  58. }
  59. }
  60. tflow += flow;
  61. for(int i = n; i != 1; i = parent[i]) {
  62. C[parent[i]][i] -= flow;
  63. C[i][parent[i]] += flow;
  64. }
  65. }
  66. }
  67. }
  68. out << tflow << "\n";
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement