Cod sursa(job #1988935)

Utilizator Emy1337Micu Emerson Emy1337 Data 5 iunie 2017 12:28:01
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
#define maxN 200010
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");

vector<pair<int,int>> G[maxN], rez;
priority_queue<pair<int,int>> Q;
int viz[maxN], c;

int main()
{
    int n,m;
    fin >> n >> m;

    while(m--)
    {
        int x, y, z;
        fin >> x >> y >> z;
        G[x].push_back({y,z});
        G[y].push_back({x,z});
    }

    Q.push({0,1});

    while(!Q.empty())
    {
        int nod = Q.top().second;
        int cost = Q.top().first;
        Q.pop();

        if(viz[nod])
            continue;

        viz[nod] = 1;
        c -= cost;
        bool flag = 1;

        for(auto it: G[nod]){
            if(!viz[it.first])
                Q.push({-it.second, it.first});
            else if(it.second == -cost && flag)
                rez.push_back({it.first, nod}), flag = 0;
        }
    }

    fout << c << "\n";
    fout << rez.size() << "\n";
    for(auto it: rez)
        fout << it.first << " " << it.second << "\n";


    return 0;
}