Cod sursa(job #1026591)

Utilizator mihaiSimuSimu Mihai mihaiSimu Data 11 noiembrie 2013 19:48:16
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <vector>
#include <queue>
#include <bitset>
#define INF 10000000
using namespace std;
int a[1003][1003];
vector<int> G[1003];
int parent[1003];
int n,m,s,t,f;


void augment_path(int u,int max_edge) {
    if(u==s) f=max_edge;
    else {
        augment_path(parent[u], min(max_edge, a[parent[u]][u]));
        a[parent[u]][u]-=f;
        a[u][parent[u]]+=f;
    }

}

int main(){
    int x,y,c;
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    cin>>n>>m;
    s=1;t=n;
    for(int i=0;i<m;i++) {
        cin>>x>>y>>c;
        a[x][y] = c;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    
    int mf = 0;
    
    while(true) {
        queue<int> q;
        bitset<1003> viz;viz.reset();
        q.push(s);
        viz[s] = 1;
        while(!q.empty()) {
            int u = q.front();q.pop();
            if(u==t) break;
            for(int i=0;i<G[u].size();i++) {
                int v = G[u][i];
                if(!viz[v] && a[u][v]>0) {
                    viz[v] = true;
                    parent[v] = u;
                    q.push(v);
                }
            }
        }
        if(!viz[t]) break;
        augment_path(t,INF);
        mf+=f;
    }
    cout<<mf;
    return 0;
}