Cod sursa(job #240775)

Utilizator EdeNNu Cred EdeN Data 8 ianuarie 2009 18:03:31
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <cstdio>
#include <vector>

using namespace std;

#define max 1005

int n, m;
int c[max][max], f[max][max];
int t[max];
bool viz[max];

vector <int> v[max];

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

bool see()
{
    int k, q[max], nod;
    q[0]=q[1]=1;
    memset(viz,0, sizeof viz);
    viz[1]=1;
    for (int i=1; i<=q[0]; i++)
    {
        nod = q[i];
        if (nod == n) continue;
        for (int i=0; i<v[nod].size(); i++)
        {
            k=v[nod][i];
            if (f[nod][k]==c[nod][k] || viz[k]) continue;
            viz[k]=1;
            q[++q[0]]=k;
            t[k]=nod;
        }
    }
    return viz[n];
}

void flux()
{
    int i, flow=0, fmin;
    while (see())
        for( i=0; i<v[n].size(); i++)
        {
            int nod=v[n][i];
            if (f[nod][n]==c[nod][n] || viz[nod]==0) continue;
            t[n] = nod;

            fmin = 110000;

            for (i=n; i!=1; i=t[i])
                fmin=min(fmin, c[t[i]][i] - f[t[i]][i]);

            for (i=n; i!=1; i=t[i])
                f[t[i]][i] += fmin,
		f[i][t[i]] -= fmin;
            flow += fmin;
        }
    printf("%d\n",flow);
}

int main()
{
    freopen("maxflow.in","rt",stdin);
    freopen("maxflow.out","wt",stdout);
    citire();
    flux();
}