Cod sursa(job #1927125)

Utilizator brczBereczki Norbert Cristian brcz Data 14 martie 2017 22:28:29
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include<bits/stdc++.h>

#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define sz size
#define pb push_back
#define mp make_pair
#define bg begin
#define nd end
using namespace std;

#define maxn 100003

int cap[1001][1001];
int flux[1001][1001];
vector<int> g[1001];
int n,m;
int father[1001];
int viz[1001];

bool bfs(){

  queue<int> q;
  q.push(1);
  memset(viz,0,sizeof(viz));
  viz[1] = 1;
  int nod=1;
  while(!q.empty()){

    nod = q.front();
    q.pop();
    if(nod == n) continue;
    for(int i=0;i<g[nod].sz();i++){
      int nn = g[nod][i];
      if(viz[nn] == 1 || cap[nod][nn] == flux[nod][nn]){
        continue;
      }
      viz[nn] = 1;
      father[nn] = nod;
      q.push(nn);
    }
  }
  return viz[n];
}



int main(){

	//ios_base::sync_with_stdio(false);

  ifstream fin("maxflow.in");
  ofstream fout("maxflow.out");

  fin >> n >> m;
  int a,b,c;

  father[1]=1;

  for(int i=0;i<m;i++){
    fin >> a >> b >> c;
    g[a].pb(b);
    g[b].pb(a);
    cap[a][b] = c;
  }
  int flow=0;

  while(bfs()){

    for(int i=0;i<g[n].sz();i++){

        int nod = g[n][i];
        if(viz[nod]==0 || cap[nod][n] == flux[nod][n]){
          continue;
        }
        int minime = (int)1e8;
        father[n] = nod;
        for(int t=n;t!=1;t=father[t]){
          minime = min(minime,cap[father[t]][t]-flux[father[t]][t]);
        }
        if(minime == 0) continue;

        for(int t=n;t!=1;t=father[t]){
          flux[father[t]][t]+=minime;
          flux[t][father[t]]-=minime;
        }

        flow+=minime;

    }
  }
  fout << flow << '\n';

	return 0;
}