Cod sursa(job #393223)

Utilizator borsoszalanBorsos Zalan borsoszalan Data 9 februarie 2010 08:50:01
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int N, M, t[200005], cost;

struct m
{
	int x, y, c;
};
vector <m> apm, v;

int cmp(struct m x, struct m y)
{
	return x.c < y.c;
}

int tata(int x)
{
	if (x != t[x]) t[x] = tata(t[x]);
	return t[x];
}

int main()
{
	freopen("apm.in","r",stdin);
	freopen("apm.out","w",stdout);
	int i, x, y;
	m aux;
	scanf("%d %d",&N,&M);
	for (i = 1; i <= M; i++)
	{
		scanf("%d %d %d",&aux.x, &aux.y, &aux.c);
		v.push_back(aux);
	}
	for (i = 1; i <= N; i++) t[i] = i;
	sort(v.begin(), v.end(), cmp);
	for (i = 0; i < M; i++)
	{
		x = tata(v[i].x); y = tata(v[i].y);
		if (x != y)
		{
			cost += v[i].c;
			t[ x ] = y;
			apm.push_back(v[i]);
		}
	}
	printf("%d\n%d\n", cost, N - 1);
	for(i = 0; i < N - 1; i++) printf("%d %d\n", apm[i].x, apm[i].y);
	return 0;
}