Cod sursa(job #813531)

Utilizator ahmed.abdraboahmed.abdrabo ahmed.abdrabo Data 15 noiembrie 2012 18:11:19
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.83 kb
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>

using namespace std;

inline int next_int() {
	int d;
	scanf("%d", &d);
	return d;
}

const int INF = 1e9;

struct MinCostMaxFlow {
	int V, E, min_cost, max_flow, path_cost, path_flow, where;
	vector<int> from, to, capacity, cost;
	MinCostMaxFlow(int n, int m) {
		V = n, E = 0, min_cost = 0, max_flow = 0;
		from.reserve(m), to.reserve(m), capacity.reserve(m), cost.reserve(m);
	}
	void add_edge(int a, int b, int c, int d) {
		E++, from.push_back(a), to.push_back(b), capacity.push_back(c), cost.push_back(+d);
		E++, from.push_back(b), to.push_back(a), capacity.push_back(0), cost.push_back(-d);
	}
	void run(const int source, const int sink) {
		vector<int> e_begin(V, -1), e_next(E);
		for (int e = 0; e < E; e++) {
			e_next[e] = e_begin[from[e]];
			e_begin[from[e]] = e;
		}
		vector<int> dist(V, INF);
		dist[source] = 0;
		for (int v = 0; v < V; v++) {
			for (int e = 0; e < E; e++) {
				int a = from[e], b = to[e], c = capacity[e], d = cost[e];
				if (dist[a] < INF && c > 0 && dist[a] + d < dist[b]) {
					dist[b] = dist[a] + d;
				}
			}
		}
		path_cost = dist[sink];
		while (true) {
			for (int e = 0; e < E; e++) {
				int a = from[e], b = to[e];
				cost[e] += dist[a] - dist[b];
			}
			vector<int> prev(V, -1), seen(V, false);
			dist.assign(V, INF);
			dist[source] = 0;
			set<pair<int, int> > Q;
			Q.insert(make_pair(0, source));
			while (!Q.empty()) {
				int current_dist = Q.begin()->first;
				int current_where = Q.begin()->second;
				Q.erase(Q.begin());
				if (seen[current_where] == false) {
					seen[current_where] = true;
					for (int e = e_begin[current_where]; e != -1; e = e_next[e]) {
						int b = to[e], c = capacity[e], d = cost[e];
						if (c > 0 && current_dist + d < dist[b]) {
							Q.erase(make_pair(dist[b], b));
							Q.insert(make_pair(current_dist + d, b));
							dist[b] = current_dist + d;
							prev[b] = e;
						}
					}
				}
			}
			if (dist[sink] == INF) {
				break;
			}
			for (where = sink, path_flow = INF; where != source; where = from[prev[where]]) {
				path_flow = min(path_flow, capacity[prev[where]]);
			}
			for (where = sink; where != source; where = from[prev[where]]) {
				capacity[prev[where] ^ 0] -= path_flow;
				capacity[prev[where] ^ 1] += path_flow;
			}
			path_cost += dist[sink];
			min_cost += path_flow * path_cost;
			max_flow += path_flow;
		}
	}
};

int main() {
	freopen("fmcm.in", "r", stdin);
	freopen("fmcm.out", "w", stdout);
	int n = next_int();
	int m = next_int();
	int s = next_int() - 1;
	int d = next_int() - 1;
	MinCostMaxFlow mcmf(n, m);
	for (int i = 0; i < m; i++) {
		int x = next_int() - 1;
		int y = next_int() - 1;
		int c = next_int();
		int z = next_int();
		mcmf.add_edge(x, y, c, z);
	}
	mcmf.run(s, d);
	printf("%d\n", mcmf.min_cost);
	return 0;
}