Cod sursa(job #689219)

Utilizator legendarulDavid Anton Erculescu legendarul Data 24 februarie 2012 10:52:24
Problema Arbore partial de cost minim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include<iostream>
#include<fstream>

using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");

struct muchie{
int x,y,c;
};
muchie vm[1500],h[800];
int n,m,ct,c[800];

void citire(){
	int i;
	fin>>n>>m;
	for(i=1;i<=m;i++)
		fin>>vm[i].x>>vm[i].y>>vm[i].c;
}
void sort(){
	int i,j;
	muchie aux;
	for(i=1;i<m;i++)
		for(j=i+1;j<=m;j++)
			if(vm[i].c>vm[j].c)
				aux=vm[i],vm[i]=vm[j],vm[j]=aux;
}
void apm(){
	int i,j;
	for(i=1;i<=n;i++)
		c[i]=i;
	sort();
	int msel=0;i=1;
	while(msel<n-1){
		while(c[vm[i].x]==c[vm[i].y])i++;
		msel++;
		h[msel]=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) max=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();
	fout<<ct<<"\n"<<n-1<<"\n";
	for(int i=1;i<n;i++)
		fout<<h[i].x<<" "<<h[i].y<<"\n";
}