Cod sursa(job #1228336)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 13 septembrie 2014 19:51:45
Problema Cuplaj maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.99 kb
#include <fstream>
#include <queue>
#include <vector>
#define DIMN 605
#define DIMM 50005
#define INF 2000000000
#define vint vector<int>::iterator
#define infile "cmcm.in"
#define outfile "cmcm.out"
#define minim(a, b) (a < b ? a : b)

using namespace std;

ifstream f(infile);
ofstream g(outfile);

vector<int> L[DIMN];

queue<int> Q;

pair<int, int> edges[DIMM];

int n, m, n1, n2, flow, a, b, c;

int F[DIMN][DIMN], C[DIMN][DIMN], cost[DIMN][DIMN], T[DIMN];

int D[DIMN];

bool ok[DIMN], o;

int BF() {
	for (int i = 0; i <= n; ++i)
		ok[i] = 0, D[i] = INF;
	D[0] = 0;
	Q.push(0);
	while (!Q.empty()) {
		int nod = Q.front();
		Q.pop();
		ok[nod] = 0;
		for (vint it = L[nod].begin(); it != L[nod].end(); ++it)
		if (D[*it] > D[nod] + cost[nod][*it] && C[nod][*it] > F[nod][*it]) {
			D[*it] = D[nod] + cost[nod][*it];
			T[*it] = nod;
			if (!ok[*it]) {
				Q.push(*it);
				ok[*it] = 1;
			}
		}
	}
	if (D[n] != INF) {
		o = true;
		int Min = INF;
		for (int i = n; i != 0; i = T[i])
			Min = minim(Min, C[T[i]][i] - F[T[i]][i]);
		flow += Min;
		for (int i = n; i != 0; i = T[i]) {
			F[T[i]][i] += Min;
			F[i][T[i]] -= Min;
		}
		return Min*D[n];
	}
	return 0;
}

int main() {
	f >> n1 >> n2 >> m;
	for (int i = 1; i <= m; ++i) {
		f >> a >> b >> c;
		b += n1;
		L[a].push_back(b);
		L[b].push_back(a);
		C[a][b] = 1;
		cost[a][b] = c;
		cost[b][a] = -c;
		edges[i] = make_pair(a, b);
	}
	for (int i = 1; i <= n1; ++i) {
		L[0].push_back(i);
		L[i].push_back(0);
		C[0][i] = 1;
	}
	n = n1 + n2 + 1;
	for (int i = n1 + 1; i<n; ++i) {
		L[n].push_back(i);
		L[i].push_back(n);
		C[i][n] = 1;
	}
	o = true;
	int res = 0;
	while (o) {
		o = false;
		res += BF();
	}
	g << flow << " " << res << "\n";
	for (int i = 1; i <= m; ++i)
	if (F[edges[i].first][edges[i].second] == 1)
		g << i << " ";
	return 0;
}

//This room. This bullet. There's a bullet for everyone. And a time. And a place. Yes... maybe this is how it has to be. - 47