Cod sursa(job #751917)

Utilizator test1Trying Here test1 Data 27 mai 2012 13:39:47
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#define INF 0x7FFFFFFF
using namespace std;

vector<int>v[1001];

int n,m,c[1001][1001],f[1001][1001],tata[1001],cd[1001];
bool viz[1001];

bool bfs(){
    int p ,u ,x ,y;
        memset(viz,0,sizeof(viz));

    viz[1] = 1;
    cd[p = u = 1] = 1;
    while(p <= u)
    {
        x = cd[p];
        for(int i=0;i<v[x].size();i++)
        {
            y = v[x][i];
            if(c[x][y] - f[x][y] > 0 && viz[y] == 0)
            {
                cd[++u] = y;
                tata[y] = x;
                viz[y] = 1;
                if(y == n) return true;
            }
        }
        p++;
    }
    return false;
}

void flux_maxim(){
    int flux_tot = 0, flux, x;
    int cnt = 0;
    for( ; bfs() ; flux = INF)
    {
        for(x = n; x != 1; x = tata[x]) flux = min(flux, c[tata[x]][x] - f[tata[x]][x]);
        flux_tot += flux;
        for(x = n; x != 1; x = tata[x])
        {
            f[tata[x]][x] += flux;
            f[x][tata[x]] -= flux;
        }
    }

    printf("%d\n",flux_tot);
}

int main(){
    int x,y,z;

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

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

    flux_maxim();
}