Mai intai trebuie sa te autentifici.
Cod sursa(job #1700725)
Utilizator | Data | 11 mai 2016 05:23:44 | |
---|---|---|---|
Problema | Arbore partial de cost minim | Scor | 60 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.67 kb |
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
//am si implementare in 100 cu heap
// e 5 dimineata, nu prea le mai vad
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
#define mult 2000000000
int n, m, x, y, z, s;
vector<pair<int,int> >l[200001];
vector<pair<int,int> >mu;
int dist[200001], t[200001];
bool viz[200001];
int main()
{
f >> n >> m;
for( int i=1; i<=m; ++i )
{
f >> x >> y >> z;
l[x].push_back(make_pair(y,z));
l[y].push_back(make_pair(x,z));
}
for( int i=1; i<=n; ++i )
dist[i] = mult;
viz[1] = true;
for( vector<pair<int,int> >::iterator it=l[1].begin(); it!=l[1].end(); ++it )
{
dist[it->first] = it->second;
t[it->first] = 1;
}
int minim, tt;
for( int i=1; i<n; ++i )
{
int j;
for( j = 1; viz[j]==true; ++j );
minim = dist[j];
tt = j;
for( ; j<=n; ++j )
if( dist[j] < minim && viz[j] != true )
{
minim = dist[j];
tt = j;
}
mu.push_back(make_pair(t[tt],tt));
s += minim;
for( vector<pair<int,int> >::iterator it=l[tt].begin(); it!=l[tt].end(); ++it )
if( dist[it->first] > it->second && viz[it->first] != true )
{
dist[it->first] = it->second;
t[it->first] = tt;
}
viz[tt] = true;
}
g << s << '\n' << n-1 << '\n';
for( vector<pair<int,int> >::iterator it=mu.begin(); it!=mu.end(); ++it )
g << it->first << " " << it->second << '\n';
return 0;
}