Cod sursa(job #2678971)

Utilizator cristina_borzaCristina Borza cristina_borza Data 29 noiembrie 2020 11:17:45
Problema Arbore partial de cost minim Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Dim = 4e5 + 5;
const int Inf = 1e9 + 5;

int dist[Dim], t[Dim];
int n, m, costTotal;

bool used[Dim];

vector <pair <int, int> > G[Dim], ans;

class comp {
public:
    bool operator() (int &t1, int &t2) {
        return dist[t1] > dist[t2];
    }
};

priority_queue<int, vector <int>, comp> coada;

int main() {
    f >> n >> m;
    for(int i = 1; i <= m; ++i) {
        int x, y, cost;
        f >> x >> y >> cost;
        G[x].push_back({y, cost});
        G[y].push_back({x, cost});
    }

    for(int i = 1; i <= n; ++i)  dist[i] = Inf;
    dist[1] = 0;

    coada.push(1);
    while(!coada.empty()) {
        int node = coada.top();
        coada.pop();

        if(used[node] == false) {
            used[node] = true;
            costTotal += dist[node];
            if(t[node]) ans.push_back({node, t[node]});

            for(auto it = G[node].begin(); it != G[node].end(); ++it) {
                if(it -> second < dist[it -> first]) {
                    dist[it -> first] = it -> second;
                    t[it -> first] = node;
                    coada.push(it -> first);
                }
            }
        }
    }

    g << costTotal << '\n' << n - 1 << '\n';
    for(auto it = ans.begin(); it != ans.end(); ++it) {
        g << it -> first << " " << it -> second << '\n';
    }
    return 0;
}