Cod sursa(job #2265040)

Utilizator slym777Darii Dan slym777 Data 20 octombrie 2018 15:37:34
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

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

int n,m,x,y,c,maxflow = 0;
int GR[1010][1010];
int tata[1010], viz[1010];
vector <int> L[1010];

int BFS()
{
    for (int i = 1 ; i <= n; i++)
        tata[i] = 0, viz[i] = 0;
    queue <int> Q;
    Q.push(1);
    tata[1] = 1;
    while (!Q.empty())
    {
        x = Q.front();
        Q.pop();
        if (x == n) continue;
        for (auto j : L[x])
            if (GR[x][j] > 0 && viz[j] == 0 && j!=x)
            {
                Q.push(j);
                viz[j] = 1;
                tata[j] = x;
            }
    }
    return viz[n];
}

int main()
{
    fin >> n >> m;
    for (int k = 0; k < m; k++)
    {
        fin >> x >> y >> c;
        GR[x][y] = c;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    while (BFS())
    {
        for (auto j:L[n])
        {
            if ( GR[j][n] == 0 || !viz[j]) continue;
            tata[n] = j;
            x = n;
            y = INT_MAX;
            while (x != 1)
            {
                y = min(GR[tata[x]][x],y);
                x = tata[x];
            }
            x = n;
            while (x != 1)
            {
                GR[tata[x]][x] -= y;
                GR[x][tata[x]] += y;
                x = tata[x];
            }
            maxflow += y;
        }
    }
    fout << maxflow;
    return 0;
}