Cod sursa(job #2168141)

Utilizator netfreeAndrei Muntean netfree Data 14 martie 2018 09:45:33
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>
#define x first
#define y second
#define pii pair<int, int>
#define piii pair<int, pair<int, int> >

using namespace std;

const int N_MAX = 200000 + 5;

vector<pii> vec[N_MAX], sol;
priority_queue<piii, vector<piii>, greater<piii> > q;


ifstream fin ("apm.in");
ofstream fout("apm.out");

bitset<N_MAX> used;

int n, m, ans;

int main()
{
    fin >> n >> m;
    while(m--){
        int a, b, c;
        fin >> a >> b >> c;
        vec[a].push_back({b,c});
        vec[b].push_back({a,c});
    }

    q.push({0, {0,1}});

    while(!q.empty()){
        int from = q.top().y.x;
        int to = q.top().y.y;
        int cost = q.top().x;

        q.pop();

        if(!used[to]){
            ans += cost;
            sol.push_back({from, to});
            for(auto v : vec[to])
                if(!used[v.x])
                    q.push({v.y, {to, v.x}});

        }

        used[to] = true;
    }

    fout << ans << "\n";
    for(auto i : sol)
        fout << i.x << " " << i.y << "\n";
    return 0;
}