Cod sursa(job #2419014)

Utilizator alexoloieriAlexandru Oloieri alexoloieri Data 7 mai 2019 13:34:52
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <bits/stdc++.h>
#define nmax 1005
#define inf 2000000000
using namespace std;
vector<int>g[nmax];
int cost[nmax][nmax], currFlow[nmax][nmax];
int drum[nmax];
int n, m, x, y, fl, flow, add, act;
bool vz[nmax];
bool bfs()
{
    memset(vz, 0, sizeof(vz));
    queue<int>q;
    vz[1] = true;
    q.push(1);
    while(!q.empty())
    {
        act = q.front();
        q.pop();
        for (auto x:g[act])
        {
            if (cost[act][x] == currFlow[act][x]) continue;
            if (vz[x]) continue;
            vz[x] = true;
            drum[x] = act;
            q.push(x);
            if (x == n) return true;
        }
    }
    return false;
}
int main()
{
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    scanf("%d %d",&n,&m);
    for (int i=1;i<=m;++i)
    {
        scanf("%d %d %d",&x,&y,&fl);
        g[x].push_back(y);
        g[y].push_back(x);
        cost[x][y] = fl;
    }
    for (flow = 0; bfs(); flow+=add)
    {
        add = inf;
        for (int nod = n; nod > 1; nod = drum[nod])
            add = min(add, cost[drum[nod]][nod] - currFlow[drum[nod]][nod]);
        for (int nod = n; nod > 1; nod = drum[nod])
        {
            currFlow[drum[nod]][nod] += add;
            currFlow[nod][drum[nod]] -= add;
        }
    }
    printf("%d\n",flow);
    fclose(stdin);
    fclose(stdout);
    return 0;
}