Mai intai trebuie sa te autentifici.

Cod sursa(job #763609)

Utilizator test666013Testez test666013 Data 2 iulie 2012 18:13:24
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 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)
            {
                was[y] = 1;
                tt[y] = x;
                if(y != n)cd[++u] = y;
            }
        }
    }
    return was[n];
}

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() )
    {
        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_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;
}