Pagini recente » Diferente pentru utilizator/funkydvd intre reviziile 20 si 56 | Statistici Marinescu Ioana (ioana_marinescu) | Monitorul de evaluare | Rating Tudor Chirila (TudorChirila11) | Cod sursa (job #460414)
Cod sursa(job #460414)
#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;
}