Cod sursa(job #2199701)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 28 aprilie 2018 19:11:37
Problema Flux maxim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.75 kb
#include <bits/stdc++.h>
#define dimn 1005
#define inf (int)(2e9)

std::ifstream f("maxflow.in");
std::ofstream g("maxflow.out");

int N;
int flux[dimn][dimn];
bool seen[dimn];
std::list <int> adj[dimn];
int flow[dimn][dimn];
int parent[dimn];

bool bfs(int start=1, int end=N) {
    std::queue<int> Q;
    for(int i=0; i<=N; i++)
    	seen[i] = 0;
    Q.push(start);
    seen[start] = 1;

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

        if (pres == end) continue;
        for (auto& vec: adj[pres]) {
            if (seen[vec] || flow[pres][vec] == flux[pres][vec]) continue;
            Q.push(vec);
            parent[vec] = pres;
            seen[vec] = 1;
        }
    } return seen[end];	//am ajuns la final? avem vreun drum? such are the questions of human existence... # P O E T I C C
}

int edmondkarp(int start=1, int end=N) {
    int res = 0;

    while (bfs(start, end)) {
        for (auto vec: adj[end]) {
            if (flow[vec][end] == flux[vec][end] || !seen[vec]) continue;

            int min_flow = inf;
            parent[end] = vec;
 			int p = end;
 			while(p!=start) {
 				min_flow = std::min(min_flow, flux[parent[p]][p]-flow[parent[p]][p]);
 				p = parent[p];
 			}

            if (min_flow == 0) continue;
            for (p=end; p!=start; p=parent[p]) {
                flow[parent[p]][p] += min_flow;
            }

            res += min_flow;
        }
    } return res;
}


void citire() {
	int M;
	f >> N >> M;
	for(int i=0, x, y; i<M; i++) {
		f >> x >> y;
		adj[x].push_back(y);
		adj[y].push_back(x);
		f >> flux[x][y];
	}
}
void rezolvare() {
	g << edmondkarp();
}

int main() {
	citire();
	rezolvare();

    return 0;
}