Cod sursa(job #689263)

Utilizator diehardNasturel Gabriel diehard Data 24 februarie 2012 12:09:01
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<iostream>
#include<fstream>
#include<stdlib>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
struct muchie{
	int x,y,c;
};
typedef muchie* pm;
muchie vm[400001],h[200000];
int n,m,ct;
int c[200000];

int fcmp(void const *a,void const *b){
	return ((pm)a)->c-((pm)b)->c;
}


void citire(){
	in>>n>>m;
	int i;
	for(i=1;i<=m;i++)
		in>>vm[i].x>>vm[i].y>>vm[i].c;
}

/*void stort(){
	int i,j;
	for(i=1;i<m;i++)
		for(j=i+1;j<=m;j++)
			if(vm[i].c>vm[j].c){
				muchie aux=vm[i];
				vm[i]=vm[j];
				vm[j]=aux;
			}
}*/

void apm(){
	int i;
	for(i=1;i<=n;i++)
		c[i]=i;
	qsort(vm+1,m,sizeof(vm[0]),fcmp);
	int mrel=0;
	i=1;
	while(mrel<n-1){
		while(c[vm[i].x]==c[vm[i].y]) i++;
		mrel++;
		h[mrel]=vm[i];
		int min=c[vm[i].x],max=c[vm[i].x];
		if(c[vm[i].y]>max)  max=c[vm[i].y];
		if(c[vm[i].y]<min) min=c[vm[i].y];
		for(int j=1;j<=n;j++)
			if(c[j]==max)
				c[j]=min;
		ct+=vm[i].c;
	}
}

int main(){
	citire();
	apm();
	out<<ct<<"\n"<<n-1<<"\n";
	for(int i=1;i<n;i++)
		out<<h[i].y<<" "<<h[i].x<<"\n";
}