Cod sursa(job #769877)

Utilizator test_666013Testez test_666013 Data 21 iulie 2012 11:10:27
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define MAX 1001
#define INF 0xffffff

vector<int>g[MAX];

int n,m,c[MAX][MAX],f[MAX][MAX],tt[MAX],cd[MAX];
bool was[MAX];

bool drum(){
    memset(was,0,sizeof(was));
    int p , u ,x ,y;
    cd[p = u = 1] = 1;
    was[1] = 1;
    while( p<=u )
    {
        x = cd[p++];
        for(int i=0;i<g[x].size();i++)
        {
            y = g[x][i];
            if( !was[y] && c[x][y] > f[x][y] )
            {
                was[y] = 1;
                tt[y] = x;
                if(y != n)cd[++u] = y;
            }
        }
    }
    return was[n];
}

void maxflow(){
    int flux_m = 0, flux;
    while( drum() )
    {
        for(int i=0;i<g[n].size();i++)
        if( was[ g[n][i] ] )
        {
            tt[n] = g[n][i];
            flux = INF;
            for(int x = n; x != 1;x = tt[x]) flux = min(flux, c[tt[x]][x]-f[tt[x]][x]);
            for(int x = n; x != 1;x = tt[x])
            {
                f[tt[x]][x] += flux;
                f[x][tt[x]] -= flux;
            }
            flux_m += flux;
        }
    }
    printf("%d\n",flux_m);
}

int main(){
    int x,y,z;
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
        scanf("%d %d",&n,&m);

        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            g[x].push_back(y);
            g[y].push_back(x);
            c[x][y] = z;
        }

    maxflow();
    return 0;
}