Cod sursa(job #2165731)

Utilizator infomaxInfomax infomax Data 13 martie 2018 13:25:13
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

#define f first
#define s second

using namespace std;

ifstream F("apm.in");
ofstream G("apm.out");

int n, m, x, y, c, ans, k, d[200005];
pair<int, int> sol[400005];
vector<pair<int, int> >a[200005];
const int inf=1<<30;
priority_queue<pair<int, int> > pq;
bool w[200005];

int main()
{
    F >> n >> m;
    for(int i = 1; i <=m;++i){
        F >> x >> y >> c;
        a[x].push_back({y, c});
        a[y].push_back({x, c});
    }
    pq.push({0, 1});
    for(int i = 1; i <= n; ++ i) d[i] = inf;
    while(!pq.empty()){
        x=pq.top().s;
        pq.pop();
        if(w[x]) continue;
        w[x] = 1;
        for(auto it:a[x])
            if(d[it.f] > it.s&&!w[it.f])
                d[it.f] = it.s, pq.push({-d[it.f], it.f}), sol[it.f]= {it.f, x};
    }
    for(int i = 1; i <= n; ++ i)
        if(d[i]!=inf) ans+=d[i];
    G << ans << '\n' << n-1 << '\n';
    for(int i=2; i <= n; ++ i)G << sol[i].f << " " << sol[i].s << '\n';
    return 0;
}