Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <vector>
  4. #include <queue>
  5. #define pb push_back
  6. #define Nmax 1005
  7. #define Mmax 5005
  8. #define INF 2147000000
  9. using namespace std;
  10.  
  11. queue< int > Q;
  12. vector< int > v[Nmax];
  13. int F[Nmax][Nmax],C[Nmax][Nmax];
  14. int prev[Nmax];
  15. int N,M,S,D;
  16.  
  17. inline int BF()
  18. {
  19. vector< int >:: iterator it;
  20. int f;
  21.  
  22. memset(prev,0,sizeof(prev));
  23. while( !Q.empty() ) Q.pop();
  24. Q.push(S);
  25.  
  26. while( ! Q.empty() )
  27. {
  28. f = Q.front();
  29. Q.pop();
  30.  
  31. if( f == D ) continue;
  32.  
  33. for( it=v[f].begin(); it!=v[f].end(); ++it)
  34. if( !prev[*it] && C[f][*it] > F[f][*it] )
  35. {
  36. prev[ *it ] = f;
  37. Q.push( *it );
  38. }
  39. }
  40.  
  41. return prev[D];
  42. }
  43.  
  44. int main()
  45. {
  46. vector< int >:: iterator it;
  47. int i,x,y,z, flow=0,wh,fmin;
  48.  
  49. freopen("maxflow.in","r",stdin);
  50. freopen("maxflow.out","w",stdout);
  51. scanf("%d%d",&N,&M);
  52. for(i=1;i<=M;++i)
  53. {
  54. scanf("%d%d%d",&x,&y,&z);
  55. v[x].pb(y);
  56. v[y].pb(x);
  57. C[x][y] = z;
  58. }
  59.  
  60. S=1; D=N;
  61. while( BF() )
  62. {
  63. for( it=v[D].begin(); it!=v[D].end(); ++it)
  64. if( prev[*it] )
  65. {
  66. prev[D] = *it;
  67. fmin = INF;
  68.  
  69. for( wh=D; wh!=S; wh=prev[wh] )
  70. fmin = min(fmin, C[prev[wh]][wh] - F[prev[wh]][wh] );
  71.  
  72. if( !fmin ) continue;
  73. flow += fmin;
  74.  
  75. for( wh=D; wh!=S; wh=prev[wh] )
  76. {
  77. F[prev[wh]][wh] += fmin;
  78. F[wh][prev[wh]] -= fmin;
  79. }
  80. }
  81. }
  82.  
  83. printf("%d\n",flow);
  84. fclose(stdin); fclose(stdout);
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement