Cod sursa(job #548540)

Utilizator BitOneSAlexandru BitOne Data 7 martie 2011 15:43:49
Problema Arbore partial de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 200011
#define oo 1<<20
#define pr pair< int, int >
#define mkpr make_pair< int, int >

using namespace std;
bool was[N_MAX];
int apm[N_MAX], d[N_MAX];
vector< pr > G[N_MAX];
vector< pr >::const_iterator it, iend;
int main( void )
{
	int N, M, i, x, y, c, s=0;
	ifstream in( "apm.in" );
	for( in>>N>>M; M; --M )
	{
		in>>x>>y>>c;
		G[x].push_back( mkpr( y, c ) );
		G[y].push_back( mkpr( x, c ) );
	}
	for( i=0; i <= N; ++i )
		apm[i]=i, d[i]=oo;
	d[1]=0;
	for( i=1; i < N; ++i )
	{
		x=0;
		for( y=1; y <= N; ++y )
			if( !was[y] && d[y] < d[x] )
				x=y;
		if( !x )
			break;
		was[x]=true;
		for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
			if( !was[it->first] && d[it->first] > it->second )
			{
				d[it->first]=it->second;
				apm[it->first]=x;
			}
	}
	ofstream out( "apm.out" );
	for( s=0, y=2; y <= N; ++y )
		s+=d[y];
	out<<s<<'\n'<<(N-1)<<'\n';
	for( y=2; y <= N; ++y )
		out<<y<<' '<<apm[y]<<'\n';
	return EXIT_SUCCESS;
}