Cod sursa(job #2421992)

Utilizator topala.andreiTopala Andrei topala.andrei Data 16 mai 2019 21:25:55
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <queue>

#define MAXN 1001
#define INF 1<<30
#define DEST N
#define START 1
using namespace std;
ifstream f("flux.in");
ofstream g("flux.out");
vector<int> G[MAXN];
int C[MAXN][MAXN], F[MAXN][MAXN];
int N, M, father[MAXN];
bool bfs() {
    int node, next;
    bool viz[MAXN] = {false};
    queue<int> Q;

    Q.push(START);
    viz[START] = true;
    while (!Q.empty()) {
        node = Q.front();
        Q.pop();
        for (int i = 0; i < G[node].size(); ++i) {
            next = G[node][i];
            if (viz[next] == false && C[node][next] != F[node][next]) {
                viz[next] = true;
                Q.push(next);
                father[next] = node;
                if (next == DEST) {
                    return true;
                }
            }
        }
    }
    return false;
}
int main()
{
    int x, y, capacity, flux_min;
    f >> N >> M;
    for (int i = 1; i <= M; ++i) {
        f >> x >> y >> capacity;
        C[x][y] = capacity;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    int flux = 0;
    while (bfs() == true) {
        flux_min = INF;
        int node = DEST;
        while (node != START) {
            flux_min = min(flux_min, C[father[node]][node] - F[father[node]][node]);
            node = father[node];
        }
        cout << flux_min << ' ';
        node = DEST;
        while (node != START) {
            F[father[node]][node] += flux_min;
            F[node][father[node]] -= flux_min;
            node = father[node];
        }
        flux += flux_min;
    }
    g << flux;
    return 0;
}