Cod sursa(job #751923)

Utilizator test1Trying Here test1 Data 27 mai 2012 14:11:33
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 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];
        if(x == n) {p++; continue;}
        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;
            }
        }
        p++;
    }
    return viz[n]; //whether node n was visited
}

void flux_maxim(){
    int flux_tot = 0, flux, x;
    int cnt = 0;
    for( ; bfs() ; )
    {
        for(int i = 0; i<v[n].size(); i++)
        if( viz[v[n][i]] )
        {
            tata[n] = v[n][i];
            flux = INF;
            for(x = n; x != 1; x = tata[x]) flux = min(flux, c[tata[x]][x] - f[tata[x]][x]);
            if( flux != 0 )
            {
                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();
}