Cod sursa(job #689271)

Utilizator LizzardStanbeca Theodor-Ionut Lizzard Data 24 februarie 2012 12:18:42
Problema Arbore partial de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include<fstream>
#include<stdlib.h>
using namespace std;

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

struct muchie
{
	int x,y,c;
};
typedef muchie* pm;
muchie vm[400001],h[200001];
int n,m,ct,c[200001];

void citire(),apm();
int fcmp(void const*,void const*);


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";
	return 0;
}

int fcmp(void const* a,void const * b)
{
	return ((pm)a)->c-((pm)b)->c;
}
void citire()
{
	fin>>n>>m;
	for(int i=1;i<=m;i++)
		fin>>vm[i].x>>vm[i].y>>vm[i].c;
}
void apm()
{
	int i,j,msel=0,min,max;
	qsort(vm+1,m,sizeof(vm[0]),fcmp);
	for(i=1;i<=n;i++)
		c[i]=i;
	i=1;
	while(msel<n-1)
	{
		while(c[vm[i].x]==c[vm[i].y])
			i++;
		h[++msel]=vm[i];
	if(c[vm[i].x]>c[vm[i].y])
	{
		min=c[vm[i].y];
		max=c[vm[i].x];
	}
	else
	{
		min=c[vm[i].x];
		max=c[vm[i].y];
	}
	for(j=1;j<=n;j++)
		if(c[j]==max)
			c[j]=min;
	ct+=vm[i].c;
	}
}