Cod sursa(job #1098713)

Utilizator deneoAdrian Craciun deneo Data 5 februarie 2014 01:29:53
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.93 kb

#include <vector>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;

#define MAXN 1001
#define inf 0x3f3f3f3f
#define forit(it, v) for (typeof ( (v).begin() ) it = (v).begin(); it != (v).end(); ++it)

int N, M, flux[MAXN][MAXN], ant[MAXN], cap[MAXN][MAXN];
int coada[MAXN];
vector<int> graph[MAXN];

int find_path(int start, int finish) {
    int coada[MAXN], used[MAXN], cur = 1;
    coada[0] = 0;
    coada[++coada[0]] = start;

    memset(ant, 0, sizeof ant);
    memset(used, 0, sizeof used);
    used[start] = 1;

    while (cur <= coada[0]) {
        int nod = coada[cur]; ++cur;

        if (nod == finish) return 1;

        for (auto it = graph[nod].begin(); it != graph[nod].end(); ++it) {
            if (!used[*it] && flux[nod][*it] < cap[nod][*it]) {
                coada[++coada[0]] = *it;
                ant[*it] = nod;
                used[*it] = 1;
            }
        }
    }

    return 0;
}

int main() {

    freopen("maxflow.in", "r", stdin);
    freopen("maxflow.out", "w", stdout);

    scanf("%d%d", &N, &M);

    srand(time(NULL));

    for (int i = 1; i <= M; ++i) {
        int x, y, c;
        scanf("%d%d%d", &x, &y, &c);
        graph[x].push_back(y); graph[y].push_back(x);
        cap[x][y] = c;

    }

    int maxflow = 0, start = 1, finish = N;

    memset (flux, 0, sizeof flux);

    while (find_path(start, finish)) {
        int aug = inf;
        for (int nod = finish; nod != start; nod = ant[nod]) {
            int a = ant[nod];
            aug = min (aug, cap[a][nod] - flux[a][nod]);
        }
        maxflow += aug;

        for (int nod = finish; nod != start; nod = ant[nod]) {
            int a = ant[nod];
            flux[a][nod] += aug;
            flux[nod][a] -= aug;
        }
    }

    printf("%d\n", maxflow);
    return 0;
}


void make_flux_graph(int start, int finish) {

}