Cod sursa(job #928714)

Utilizator ELHoriaHoria Cretescu ELHoria Data 26 martie 2013 17:25:47
Problema Cuplaj maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.61 kb
#include <cstdio>
#include <bitset>

using namespace std;

const int NMAX = 300, inf = int(1e9);
int n, cost[NMAX][NMAX], ax[NMAX], ay[NMAX], mch[NMAX];
bitset<NMAX> vx, vy;
unsigned short edgeIndex[NMAX][NMAX], edges[NMAX];
int minCost;
int N, M, E;

bool dfs(int v) {
	vx[v] = 1;
	for(int w = 0; w < n; w++){
		if(!vy[w] && ax[v] + ay[w] - cost[v][w] == 0){
			vy[w] = 1;
			if(mch[w] < 0 || dfs(mch[w])) {
				mch[w] = v;
				return 1;
			}
		}
	}
	return 0;
}

void khun(){
	for(int i = 0; i < n; i++) {
		mch[i] = -1;
		for(int j = 0; j < n; j++) {
			ax[i] = max(ax[i], cost[i][j]);
		}
	}
	for(int i = 0; i < n; i++){
		while(!dfs(i)){
			int d = inf;
			for(int j = 0; j < n; j++) {
				if(!vy[j]) {
					for(int k = 0; k < n; k++) {
						if(vx[k]) {
							d = min(d, ax[k] + ay[j] - cost[k][j]);
						}
					}
				}
			}
			for(int j = 0; j < n; j++) {
				if(vx[j]) ax[j] -= d, vx[j] = 0;
				if(vy[j]) ay[j] += d, vy[j] = 0;
			}
		}
	}
}

int main ()
{
	freopen("cmcm.in","r",stdin);
	freopen("cmcm.out","w",stdout);
	scanf("%d %d %d",&N,&M,&E);
	n = max(N,M);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			cost[i][j] = -inf;
		}
	}

	for(int a, b, c, i = 0;i < E;i++) {
		scanf("%d %d %d",&a,&b,&c);
		a--;b--;
		cost[a][b] = -c;
		edgeIndex[a][b] = i + 1;
	}

	khun();

	for(int i = 0; i < n; i++) {
		if(cost[mch[i]][i] != -inf) {
			minCost += cost[mch[i]][i];
			edges[++edges[0]] = edgeIndex[mch[i]][i];;
		}
	}
	printf("%d %d\n",edges[0],-minCost);
	for(int i = 1;i <= edges[0];i++) {
		printf("%d ",edges[i]);
	}
	return 0;
}