Cod sursa(job #1462455)

Utilizator EpictetStamatin Cristian Epictet Data 18 iulie 2015 10:04:55
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

#define Max_Node 1024
#define INF 110000

using namespace std;

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

int N, M, max_flow, curent_flow;
int C[Max_Node][Max_Node], F[Max_Node][Max_Node];
int Dad[Max_Node];
bool Viz[Max_Node];
vector < int > V[Max_Node];

bool BFS()
{
    queue < int > Q;
    Q.push(1);
    memset(Viz, 0, sizeof(Viz));
    Viz[1] = true;

    while (!Q.empty())
    {
        int nod = Q.front();
        Q.pop();

        for (vector < int > :: iterator it = V[nod].begin(); it != V[nod].end(); it++)
        {
            if (C[nod][*it] == F[nod][*it] || Viz[*it]) continue;
            Viz[*it] = true;
            Q.push(*it);
            Dad[*it] = nod;
            if (*it == N) return true;
        }
    }

    return false;
}

int main()
{
    fin >> N >> M;
    for (int x, y, c, i = 1; i <= M; i++)
    {
        fin >> x >> y >> c;
        C[x][y] = c;
        V[x].push_back(y);
        V[y].push_back(x);
    }

    while (BFS())
    {
        curent_flow = INF;
        for (int nod = N; nod != 1; nod = Dad[nod])
            curent_flow = min(curent_flow, C[Dad[nod]][nod] - F[Dad[nod]][nod]);

        for (int nod = N; nod != 1; nod = Dad[nod])
        {
            F[Dad[nod]][nod] += curent_flow;
            F[nod][Dad[nod]] -= curent_flow;
        }

        max_flow += curent_flow;
    }

    fout << max_flow << '\n';
    fin.close();
    fout.close();
    return 0;
}