Cod sursa(job #2603880)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 21 aprilie 2020 09:55:02
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
const int N = 1005;
int Cap[N][N],Flow[N][N],from[N];
vector<int> v[N];
void BFS(int sink)
{
    queue<int> q;
    q.push(1);
    memset(from,0,sizeof(from));
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        for (auto it: v[now])
            if (Cap[now][it]-Flow[now][it]>0 && !from[it])
            {
                from[it] = now;
                q.push(it);
            }
    }
}
int main()
{
    int n,m;
    in >> n >> m;
    for (int i = 1; i<=m; i++)
    {
        int x,y,c;
        in >> x >> y >> c;
        v[x].push_back(y);
        v[y].push_back(x);
        Cap[x][y]+=c;
    }
    int ans = 0;
    bool ok = 0;
    do
    {
        BFS(n);
        ok = 0;
        if (from[n])
        {
            ok = 1;
            for (auto last: v[n])
            {
                if (from[last] && Cap[last][n]-Flow[last][n]>0)
                {
                    int flowmin = INT_MAX;
                    for (int i = n; i!=1; i = from[i])
                        flowmin = min(flowmin,Cap[from[i]][i]-Flow[from[i]][i]);
                    ans+=flowmin;
                    for (int i = n; i!=1; i = from[i])
                    {
                        Flow[from[i]][i]+=flowmin;
                        Flow[i][from[i]]-=flowmin;
                    }
                }
            }
        }
    }
    while(ok);
    out << ans;
}