Cod sursa(job #2216642)

Utilizator popabogdanPopa Bogdan Ioan popabogdan Data 27 iunie 2018 15:09:54
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

#define Nmax 1005

using namespace std;

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

int N, M;
int C[Nmax][Nmax], F[Nmax][Nmax];
vector <int> G[Nmax];
int T[Nmax];
queue <int> Q;
int maxFlow;

bool BFS(int start, int finish)
{
    memset(T, 0, sizeof(T));
    T[start] = start;
    Q.push(start);
    while(!Q.empty())
    {
        int node = Q.front();
        Q.pop();
        for(auto it : G[node])
            if(!T[it] && C[node][it] > F[node][it])
            {
                T[it] = node;
                Q.push(it);
            }
    }
    return T[finish] != 0;
}

int main()
{
    fin >> N >> M;
    while(M--)
    {
        int x, y, c;
        fin >> x >> y >> c;
        G[x].push_back(y);
        G[y].push_back(x);
        C[x][y] = c;
    }
    while(BFS(1, N))
    {
        int add = 1000000000;
        for(int node = N; node != 1; node = T[node])
            add = min(add, C[T[node]][node] - F[T[node]][node]);
        maxFlow += add;
        for(int node = N; node != 1; node = T[node])
            F[T[node]][node] += add, F[node][T[node]] -= add;
    }
    fout << maxFlow << "\n";
    return 0;
}