Cod sursa(job #2265367)

Utilizator slym777Darii Dan slym777 Data 20 octombrie 2018 23:53:02
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 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];

bool BFS()
{
    for (int i = 1 ; i <= n; i++)
        tata[i] = 0;
    queue <int> Q;
    Q.push(1);
    tata[1] = 1;
    while (!Q.empty())
    {
        x = Q.front();
        Q.pop();
        for (int j = 1; j <= n; j++)
            if (GR[x][j] > 0 && tata[j] == 0 && j!=x)
            {
                Q.push(j);
                tata[j] = x;
                if (j == n)
                    return true;
            }
    }
    return false;
}

int main()
{
    fin >> n >> m;
    for (int k = 0; k < m; k++)
    {
        fin >> x >> y >> c;
        GR[x][y] = c;
    }
    while (BFS())
    {
        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;
}