Cod sursa(job #973485)

Utilizator tudorv96Tudor Varan tudorv96 Data 14 iulie 2013 16:49:20
Problema Flux maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.91 kb
#include <fstream>
#include <queue>
#include <cstring>
using namespace std;
 
ifstream fin ("fmcm.in");
ofstream fout ("fmcm.out");
 
#define oo 0x3f3f3f3f
#define min(a,b) ((a < b) ? a : b)
#define N 355
 
vector <int> g[N];
int C[N][N], cost[N][N], ci[N], cr[N], t[N], dr[N], n, m, s, d, sol;
bool q[N];
priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > H;
queue <int> Q;
 
inline void bellmanford() {
	Q.push(s);
	q[s] = 1;
	dr[s] = 0;
	while (Q.size()) {
		int x = Q.front();
		Q.pop();
		for (vector <int> :: iterator it = g[x].begin(); it != g[x].end(); ++it)
			if (C[x][*it] && dr[x] + cost[x][*it] < dr[*it]) {
				dr[*it] = dr[x] + cost[x][*it];
				if (!q[*it]) {
					q[*it] = 1;
					Q.push(*it);
				}
			}
	}
}
 
inline bool dijkstra() {
	memset (ci, oo, sizeof(ci));
	cr[s] = ci[s] = 0;
	H.push(make_pair (0, s));
	while (H.size()) {
		pair <int, int> crt = H.top();
		H.pop();
		if (crt.first > ci[crt.second])
			continue;
		for (vector <int> :: iterator it = g[crt.second].begin(); it != g[crt.second].end(); ++it) {
			int opt = ci[crt.second] + cost[crt.second][*it] + dr[crt.second] - dr[*it];
			if (C[crt.second][*it] && opt < ci[*it]) {
				ci[*it] = opt;
				cr[*it] = cr[crt.second] + cost[crt.second][*it];
				H.push(make_pair (ci[*it], *it));
				t[*it] = crt.second;
			}
		}
	}
	return (ci[d] != oo);
}
 
int main() {
	fin >> n >> m >> s >> d;
	memset (dr, oo, sizeof(dr));
	while (m--) {
		int x, y, c, z;
		fin >> x >> y >> c >> z;
		C[x][y] = c;
		cost[x][y] = z;
		cost[y][x] = -z;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	fin.close();
	bellmanford();
	while (dijkstra()) {
		memcpy (dr, cr, sizeof(dr)); //linia de cod care face tot fluxu optim.
		int flux = oo;
		for (int x = d; x != s; x = t[x])
			flux = min(flux, C[t[x]][x]);
		sol += flux * cr[d];
		for (int x = d; x != s; x = t[x]) {
			C[t[x]][x] -= flux;
			C[x][t[x]] += flux;
		}
	}
	fout << sol;
	fout.close();
}