Cod sursa(job #1692360)

Utilizator PTAdrian64Pop-Tifrea Adrian PTAdrian64 Data 20 aprilie 2016 19:02:16
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <cstdio>
#include <vector>
#define nmax 1001
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#include <algorithm>

using namespace std;

int N,M;
int cap[nmax][nmax],pred[nmax],F[nmax][nmax];
vector <int> G[nmax];
bool viz[nmax];
queue <int> Q;

bool BFS(){

  Q.push(1);
  memset(viz,0,sizeof(viz));
  int x;
  viz[1] = 1;
  while(!Q.empty()){
    x = Q.front();
    Q.pop();
    for(vector <int> :: iterator it = G[x].begin();it != G[x].end();++it){
      if(cap[x][*it] == F[x][*it] || viz[*it] == 1)continue;
	pred[*it] = x;
	viz[*it] = 1;
	if(*it == N)return true;
	Q.push(*it);      
    }
  }

  return false;
  
}

int main(){

  freopen("maxflow.in","r",stdin);
  freopen("maxflow.out","w",stdout);

  scanf("%d %d ",&N,&M);
  int x,y,z;
  
  while(M--){
    scanf("%d %d %d ",&x,&y,&z);
    cap[x][y] += z;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  int fmin,flow = 0;
  
  while(BFS()){

    fmin = INF;
    
    y = N;
    while(y != 1){

      fmin = min(fmin,cap[pred[y]][y] - F[pred[y]][y]);
      y = pred[y];
      
      }

    y = N;
    while(y != 1){

      F[pred[y]][y] += fmin;
      F[y][pred[y]] -= fmin;
      y = pred[y];
      
      }

    flow += fmin;
    
    }

    printf("%d\n",flow);
  
  return 0;
  
}