Cod sursa(job #683027)

Utilizator AlikingAlin Mogis Aliking Data 19 februarie 2012 21:03:10
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include<fstream.h>
#include<iostream.h>
ifstream f("apm.in");ofstream go("apm.out");
int n,m;
struct graf	{int x,y,c;} g[2000],aj;
void citire()
{
	int i=1;f>>n>>m;for(i=1;i<=m;i++)	f>>g[i].x>>g[i].y>>g[i].c;
}
void sortare()
{
	int i,j;
	for(i=1;i<m;i++) 
	{for(j=i+1;j<=m;j++)	
	{if(g[i].c>g[j].c)	
	{aj=g[i];g[i]=g[j];g[j]=aj;}}}
}
void pro()
{
	int L[2000],i,j,k=0,s=0;
	for(i=1;i<=n;i++) L[i]=i;
	i=1;
	while(k<n-1)
	{
		if(L[g[i].x]!=L[g[i].y])	
		{
			L[g[i].x]=L[g[i].y];k++;s+g[i].c;
			for(j=1;j<=m;j++) { if(L[j]==g[i].x)	L[i]=g[i].y;}
		}
		i++;
	}
	i=1;
	while(k<n-1)
	{
		if(L[g[i].x]!=L[g[i].y])	{L[g[i].x]=L[g[i].y];k++;s+g[i].c;go<<g[i].x<<' '<<g[i].y<<endl;
		for(j=1;j<=m;j++) { if(L[j]==g[i].x)	L[i]=g[i].y;}}
		i++;
	}
}
int main()
{
	citire();sortare();pro();return 0;
}