Cod sursa(job #2135669)

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

using namespace std;

const int mxn = 200 * 1000 + 10;

priority_queue <pp2, vector < pp2 >, greater< pp2 > > 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, make_pair( 1, it.x )));
    int nod, cost, nod1;
    while(!q.empty() && nr < n){
        nod = q.top().y.y;
        nod1 = q.top().y.x;
        cost = q.top().x;
        q.pop();
        if(viz[ nod ] == 0){
            sol.push_back(make_pair(nod1, nod));
            nr++;
            s += cost;
            viz[ nod ] = 1;
            for(auto it:v[ nod ])
                if(viz[ it.x ] == 0)
                    q.push(make_pair( it.y, make_pair(nod, 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';
    for(int i = 0; i < sol.size(); i++)
        cout<< sol[ i ].x << ' ' << sol[ i ].y << '\n';
    return 0;
}