Cod sursa(job #2297744)

Utilizator HumikoPostu Alexandru Humiko Data 6 decembrie 2018 14:52:26
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.03 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");

int n, m, answer;

int previous[1005];

int flux[1005][1005];
int capacity[1005][1005];

bitset <1005> f;
vector <int> graph[1005];

void reset()
{
    f.reset();
    for ( int i = 1; i <= n; ++i )
        previous[i] = 0;
}

bool maximumFlux ( int source )
{
    queue <int> solutions;

    f[source] = 1;
    solutions.push(source);

    while ( solutions.size() )
    {
        int node = solutions.front();
        solutions.pop();

        if ( node == n )
            continue;

        for ( auto x:graph[node] )
        {
            if ( !f[x] && flux[node][x] < capacity[node][x] )
            {
                previous[x] = node;
                f[x] = 1;
                solutions.push(x);
            }
        }
    }

    return f[n];
}

int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0); fout.tie(0);

    fin>>n>>m;
    for ( int i = 1; i <= m; ++i )
    {
        int x, y, flucsuwu;
        fin>>x>>y>>flucsuwu;

        graph[x].push_back(y);
        graph[y].push_back(x);
        capacity[x][y] += flucsuwu;
    }

    while ( maximumFlux(1) )
    {
        for ( auto x:graph[n] )
        {
            if ( f[x] && capacity[x][n] > flux[x][n] )
            {
                previous[n] = x;
                int aux = n;
                int minimum = 1e9;

                while ( aux != 1 )
                {
                    minimum = min(minimum, capacity[previous[aux]][aux]-flux[previous[aux]][aux]);
                    aux = previous[aux];
                }

                aux = n;

                while ( aux != 1 )
                {
                    flux[previous[aux]][aux] += minimum;
                    flux[aux][previous[aux]] -= minimum;
                    aux = previous[aux];
                }

                answer += minimum;
            }
        }

        reset();
    }

    fout<<answer<<'\n';
}