Advertisement
kcku

00820 - Internet Bandwidth

Aug 14th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     for(int n, s, t, c, cnt=0; scanf("%d", &n) && n && scanf("%d%d%d", &s, &t, &c);)
  10.     {
  11.         int C[101][101]={}, F[101][101]={}, f=0;
  12.  
  13.         for(int x, y, z; c-- && scanf("%d%d%d", &x, &y, &z);)
  14.             C[x][y]+=z, C[y][x]+=z;
  15.  
  16.         while(1)
  17.         {
  18.             int P[101], M[101]={};
  19.             memset(P, -1, 404);
  20.             P[s]=-2, M[s]=1e9;
  21.             queue<int> q;
  22.             q.push(s);
  23.  
  24.             while(!q.empty() && !M[t])
  25.             {
  26.                 int p=q.front();
  27.                 q.pop();
  28.  
  29.                 for(int i=1; i<=n; i++)
  30.                     if(C[p][i]>F[p][i] && P[i]==-1)
  31.                         q.push(i), P[i]=p, M[i]=min(M[p], C[p][i]-F[p][i]);
  32.             }
  33.             if(M[t]==0) break;
  34.             f+=M[t];
  35.  
  36.             for(int i=t, j=P[i]; i!=s; j=P[i=j])
  37.                 F[j][i]+=M[t], F[i][j]-=M[t];
  38.         }
  39.         printf("Network %d\nThe bandwidth is %d.\n\n", ++cnt, f);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement