Cod sursa(job #2408226)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 17 aprilie 2019 18:56:40
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>
#define NM 200005
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
void read();
struct edge
{
    int x, y, c;
};
struct cmp
{
    bool operator()(const edge& a, const edge& b)
    {
        return a.c > b.c;
    }
};
void read();
int n, m, s;
bitset<NM> viz;
vector<pair<int,int>> v[NM], sol;
priority_queue<edge, vector<edge>, cmp> q;
int main()
{
    read();
    for(auto it : v[1])
        q.push({1, it.first, it.second});
    viz[1] = 1;
    while(!q.empty())
    {
        edge cur = q.top();
        q.pop();
        if(viz[cur.y])
            continue;
        else
        {
            sol.push_back({cur.x, cur.y});
            s+=cur.c;
            viz[cur.y] = 1;
            for(auto it : v[cur.y])
                if(!viz[it.first])
                    q.push({cur.y, it.first, it.second});
        }
    }
    fout << s << '\n' << n-1 << '\n';
    for(auto it : sol)
        fout << it.first << ' ' << it.second << '\n';
    return 0;
}
void read()
{
    fin >> n >> m;
    for(int i=1; i<=m; i++)
    {
        int x, y, c;
        fin >> x >> y >> c;
        v[x].push_back({y, c});
        v[y].push_back({x, c});
    }
}