Cod sursa(job #781532)

Utilizator my666013Test Here my666013 Data 24 august 2012 16:43:11
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define Max 1001
#define Inf 0xffffff

vector<int>g[Max];
int c[Max][Max],f[Max][Max],n,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;

    while(p <= u)
    {
        x = cd[p++];
      //  if(x == n)continue;
        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 max_flow(){
    int flux_max = 0, flux ,step = 0;

    while( bfs() )
    {
        for(int i=0;i<g[n].size();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]);
            if(flux != 0)
            {
                flux_max += flux;
                for(int x = n;x != 1;x = tt[x])
                {
                    f[tt[x]][x] += flux;
                    f[x][tt[x]] -= flux;
                }
            }
        }
    }

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

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

    freopen("flux.in","r",stdin);
    freopen("flux.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;
        }

    max_flow();

    return 0;
}