Cod sursa(job #541446)

Utilizator PopescuMihaiPopescu Mihai-Mihai PopescuMihai Data 25 februarie 2011 11:26:25
Problema Lazy Scor 0
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.1 kb
#include<cstdio>
#include<algorithm>

using namespace std;

#define Nmax 200001

struct muchie {
	int x, y, cost, profit, ind;
} V[Nmax];

int N, M, T[Nmax], val, sol[Nmax*2];

int cmp(muchie a, muchie b) {
	if(a.cost!=b.cost)
		return a.cost<b.cost;
	else
		return a.profit>b.profit;
}

int get_root(int x) {
	while(T[x]!=x)
		x=T[x];
	return x;
}

int main() {
	freopen("lazy.out","w",stdout);
	freopen("lazy.in","r",stdin);
	
	int i, j, a, b, c, d, k, root1, root2;
	scanf("%d %d",&N,&M);
	for(i=1; i<=M; i++) {
		scanf("%d %d %d %d",&a,&b,&c,&d);
		V[i].x=a; V[i].y=b; 
		V[i].cost=b;
		V[i].profit=d-(c*d);
		V[i].ind=i;
		T[i]=i;
	}
	sort(V+1,V+M+1,cmp);
	for(k=1; k<=M && val<N-1; k++) {
		i=V[k].x; j=V[k].y; c=V[k].cost;
		root1=get_root(i);
		root2=get_root(j);
		if(root1==root2)
			continue;
		if(root1>root2)
			T[root1]=T[root2];
		else
			T[root2]=T[root1];
		sol[++val]=V[k].ind;	
	}
	
	/*for(i=1; i<=M; i++)
		printf("%d %d %d %d %d\n",V[i].x,V[i].y,V[i].cost,V[i].profit,V[i].ind);*/
	
	for(i=1; i<=val; i++)
		printf("%d\n",sol[i]);
	
	return 0;
}