Cod sursa(job #460414)

Utilizator BitOneSAlexandru BitOne Data 2 iunie 2010 15:15:08
Problema Arbore partial de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <vector>
#include <cstdlib>
#include <fstream>
#define Nmax 200111
#define oo 9999999

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