Cod sursa(job #763607)

Utilizator test666013Testez test666013 Data 2 iulie 2012 18:09:06
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
#define MAX 1001
#define INF 0xffffff

vector<int>g[MAX];

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

bool bfs(){
    int p,u,x,y;

    memset(was,0,sizeof(was));
    cd[p = u = 1] = 1;
    was[1] = 1; //nodul sursa a fost vizitat

    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] > 0)
            {
                tt[y] = x;
                cd[++u] = y;
                if(y == n)return 1;
            }
        }
    }
    return 0; // nu am gasit un drum
}

int main(){
    int m,x,y,z;
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
        scanf("%d %d",&n,&m);
        while(m--)
        {
            scanf("%d %d %d",&x,&y,&z);
            g[x].push_back(y);
            g[y].push_back(x);
            c[x][y] = z;
        }
    int flux_maxim = 0 , flux ;

    while( bfs() )
    {
        flux = INF;
        for(int x=n;x!=1;x=tt[x]) flux = min(flux,c[tt[x]][x] - f[tt[x]][x]);
        if( flux != 0 )
        {
            flux_maxim += flux;
            for(int x=n;x!=1;x=tt[x])
            {
                f[tt[x]][x] += flux;
                f[x][tt[x]] -= flux;
            }
        }
    }
        printf("%d\n",flux_maxim);
    return 0;
}