Cod sursa(job #2098738)

Utilizator infomaxInfomax infomax Data 3 ianuarie 2018 14:27:11
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>

using namespace std;

ifstream F("maxflow.in");
ofstream G("maxflow.out");

int t[1002], c[1002][1002], f[1002][1001], x, y, z, n, m, Fmin, flow;
bitset<1002> v;
vector<int> a[1002];
queue<int> q;
const int inf = 1 << 30;

int bfs()
{
    q.push(1); v = 0; v[1] = 1;
    vector<int> :: iterator it;
    while(!q.empty())
    {
        x = q.front();q.pop();
        if(x == n) continue;
        for(it = a[x].begin(); it != a[x].end(); ++ it)
        {
            y = *it;
            if(c[x][y] == f[x][y] || v[y]) continue;
            v[y] = 1;
            q.push(y);
            t[y] = x;
        }
    }
    return v[n];
}

int main()
{
    F >> n >> m;
    for( int i = 1; i <= m; ++ i )
    {
        F >> x >> y >> z;
        c[x][y] = z;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    vector<int> :: iterator it;
    while(bfs())
        for( it = a[n].begin(); it != a[n].end(); ++ it )
        {
            y = *it;
            if( c[y][n] == f[y][n] || !v[y] ) continue;
            t[n] = y;

            Fmin = inf;
            for( int i = n; i > 1; i = t[i] )
            {
                Fmin = min(Fmin, c[t[i]][i] - f[t[i]][i]);
            }
            if(!Fmin) continue;
            for( int i = n; i > 1; i = t[i] ) f[t[i]][i] += Fmin, f[i][t[i]] -= Fmin;
            flow += Fmin;
        }
    G << flow;
    return 0;
}