Cod sursa(job #2431819)

Utilizator DariusDCDarius Capolna DariusDC Data 20 iunie 2019 20:27:45
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.78 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 flux[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];
        if (nod == n)
            continue;
        for (int j = 0; j < G[nod].size(); j++)
        {
            int vecin = G[nod][j];
            if (cost[nod][vecin] == flux[nod][vecin] || viz[vecin] == true)
                continue;
            cd[ ++cd[0] ] = vecin;
            tata[vecin] = nod;
            viz[vecin] = true;
        }
    }
    return viz[n];
}

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())
    {
        for (int j = 0; j < G[n].size(); j++)
        {
            int fluxmin = inf;
            int nod = G[n][j];
            if (!viz[nod] || cost[nod][n] == flux[nod][n])
                continue;
            tata[n] = nod;
            for (int NOD = n; NOD != 1; NOD = tata[NOD])
            {
                fluxmin = min(fluxmin, cost[tata[NOD]][NOD] - flux[tata[NOD]][NOD]);
            }
            for (int NOD = n; NOD != 1; NOD = tata[NOD])
            {
                flux[tata[NOD]][NOD] += fluxmin;
                flux[NOD][tata[NOD]] -= fluxmin;
            }
            flow += fluxmin;
        }
    }
    fout << flow;
    return 0;
}