Cod sursa(job #2431468)

Utilizator DariusDCDarius Capolna DariusDC Data 19 iunie 2019 17:01:52
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#define LIM 1005
#define inf (1 << 29)
#define pb push_back

using namespace std;

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

int n, m;

vector <int> G[LIM];

int tata[LIM];
bool viz[LIM];
int invers[LIM][LIM];
int cost[LIM][LIM];
int cd[LIM];

int flow;

bool BFS()
{
    cd[0] = 1;
    cd[1] = 1;
    memset(viz, 0, sizeof(viz));
    viz[1] = true;
    for (int i = 1; i <= cd[0]; i++)
    {
        int nod = cd[i];
        for (int j = 0; j < G[nod].size(); j++)
        {
            int vecin = G[nod][j];
            if (cost[nod][vecin] == invers[nod][vecin] || viz[vecin] == true)
                continue;
            cd[ ++cd[0] ] = vecin;
            tata[vecin] = nod;
            viz[vecin] = true;
            if (vecin == n)
                return true;
        }
    }
    return false;
}

int main()
{
    fin >> n >> m;
    while (m--)
    {
        int x, y, z;
        fin >> x >> y >> z;
        G[x].pb(y);
        G[y].pb(x);
        cost[x][y] += z;
    }
    while (BFS())
    {
        int flowmin = inf;
        for (int nod = n; nod != 1; nod = tata[nod])
        {
            flowmin = min(flowmin, cost[tata[nod]][nod] - invers[tata[nod]][nod]);
        }
        for (int nod = n; nod != 1; nod = tata[nod])
        {
            invers[tata[nod]][nod] += flowmin;
            invers[nod][tata[nod]] -= flowmin;
        }
        flow += flowmin;
    }
    fout << flow;
    return 0;
}