Cod sursa(job #2135665)

Utilizator CronosClausCarare Claudiu CronosClaus Data 19 februarie 2018 00:15:20
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>
#define x first
#define y second
#define pp pair <int, int>

using namespace std;

const int mxn = 200 * 1000 + 10;

priority_queue <pp, vector < pp >, greater< pp > > q;

vector< pp > v[ mxn ];
vector< pp > sol;

int n, m;
int nr = 1;
int s;

bool viz[ mxn ];

void solve(){
    viz[ 1 ] = 1;
    for(auto it:v[ 1 ])
        q.push(make_pair(it.y, it.x));
    int nod, cost;
    while(!q.empty() && nr < n){
        nod = q.top().y;
        cost = q.top().x;
        q.pop();
        if(viz[ nod ] == 0){
            nr++;
            s += cost;
            viz[ nod ] = 1;
            for(auto it:v[ nod ])
                if(viz[ it.x ] == 0)
                    q.push(make_pair( it.y, it.x ));
        }
    }
}

int main()
{
    ios_base::sync_with_stdio( false );
    cin.tie( 0 );
    ifstream cin("apm.in");
    ofstream cout("apm.out");
    cin>> n >> m;
    for(int i = 0, xx, yy, zz; i < m; ++i){
        cin>> xx >> yy >> zz;
        v[ xx ].push_back(make_pair(yy, zz));
        v[ yy ].push_back(make_pair(xx, zz));
    }
    solve();
    cout<< s << '\n' << nr - 1 << '\n';
    return 0;
}