Cod sursa(job #3205360)

Utilizator AdyCiubotaruCiubotaru Ady AdyCiubotaru Data 19 februarie 2024 13:49:58
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>


using namespace std;

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

vector <pair<int , int>> G[400001];

int n , m , x , y , c , S;
vector<int> D;
vector<bool>V;
pair<int,int>mem[400001];
int nr;
int main()
{
    for(in >> n >> m ; m ; --m)
    {
        in >> x >> y >> c;
        G[x].push_back({c , y});
        G[y].push_back({c , x});
    }

    priority_queue <
        pair<int , int>  ,
        vector<pair<int , int>> ,
        greater<pair<int , int>>
    >Q;

    V.resize(n + 1 , false);
    D.resize(n + 1 , 0x3f3f3f3f);

    V[1] = true;
    D[1] = 0;

    for(auto x : G[1])
    {
        D[x.second] = x.first;
        Q.push(x);
    }

    for(int k = 1 ; k < n ; k++)
    {
        pair<int , int> P;
        do{
            P = Q.top();
            Q.pop();
        }while(V[P.second]);
        V[P.second] = true;
        S += P.first;
        ++nr;
        mem[nr].first = P.second;
        for(auto x : G[P.second])
            if(V[x.second] == false && D[x.second] > x.first)
            {
                D[x.second] = x.first;

                Q.push(x);
            }
    }

    out << S << '\n';
    out<<nr<<'\n';
    for(int i = 1;i<=nr;i++)
    out<<mem[i].first<<" ";
}