Pagini recente » Cod sursa (job #3353233) | Cod sursa (job #335272) | Cod sursa (job #3348541) | Cod sursa (job #3344384) | Cod sursa (job #548540)
Cod sursa(job #548540)
#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;
}