Cod sursa(job #344382)

Utilizator AstronothingIulia Comsa Astronothing Data 29 august 2009 21:15:21
Problema Flux maxim Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
//beta version
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int g[1002][1002],n,m,rezid[1002][1002],source,dest;
vector<int> list[1002];
int maxf;

int find_path()
{
    queue<int> q;
    int vis[1002],dad[1002];
    memset(vis,0,sizeof(vis));
    memset(dad,0,sizeof(dad));
    q.push(source);
    vis[source] = 1;
    dad[source] = -1;
    bool ok = 0;
    while (!q.empty())
    {
        int crt = q.front();
        q.pop();
        if (crt == dest) break;
        for (int i=0;i<list[crt].size();++i)
            if (!vis[list[crt][i]] && rezid[crt][list[crt][i]]>0)
            {
                if(list[crt][i] == dest) ok = 1;
                vis[list[crt][i]] = 1;
                dad[list[crt][i]] = crt;
                q.push(list[crt][i]);
            }
        if(ok) break;
    }
    if(dad[dest]==0) return 0;

    //gasesc minimul pe care-l pot trimite pe calea curenta
    int crt = dest;
    int flow = maxf;
    while(dad[crt]!=-1)
    {
        if(rezid[dad[crt]][crt]<flow) flow = rezid[dad[crt]][crt]<flow;
        crt = dad[crt];
    }

    //si-l trimit
    crt = dest;
    while(dad[crt]!=-1)
    {
        rezid[dad[crt]][crt] -= flow;
        rezid[crt][dad[crt]] += flow;
        crt = dad[crt];
    }
    return flow;
}

int maxflow()
{
    int res = 0;
    while (13)
    {
        int aug = find_path();
        if (aug<1) return res;
        res += aug;
    }
}

int main()
{
    fstream f("maxflow.in",ios::in);
    fstream f2("maxflow.out",ios::out);
    int x,y,c;
    f>>n>>m; //>>source>>dest;
    source = 1;
    dest = n;
    while (f>>x>>y>>c) g[x][y] = rezid[x][y] = c, list[x].push_back(y), maxf = c>maxf ? c : maxf;

    f2<<maxflow();

    return 0;
}