Cod sursa(job #2417723)

Utilizator hiimsobaDicianu Ioan-Alexandru hiimsoba Data 30 aprilie 2019 22:01:07
Problema Arbore partial de cost minim Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

struct Edge {
    int from, to, cost;
};

int rad(std::vector<int>& P, int x) {
    int y = x;
    while (x != P[x]) {
        x = P[x];
    }
    while (P[y] != x) {
        int aux = P[y];
        P[y] = x;
        y = aux;
    }
    return x;
}

int main() {
    std::ifstream f("apm.in");
    int V, E;
    f >> V >> E;
    std::vector<Edge> Graph(E);
    for (int i = 0; i < E; ++i) {
        int x, y, c;
        f >> x >> y >> c;
        Graph[i] = Edge {x, y, c};
    }
    f.close();
    std::sort(Graph.begin(), Graph.end(), [] (Edge& a, Edge& b) {return a.cost < b.cost;});
    std::vector<int> P(V);
    for (size_t i = 0; i < P.size(); i++) P[i] = i;
    std::vector<std::pair<int, int>> res(V - 1);
    int k = 0;
    int cost = 0;
    for (int i = 0; i < E && k < V - 1; i++) {
        int x = rad(P, Graph[i].from);
        int y = rad(P, Graph[i].to);
        if (x == y) continue;
        P[x] = y;
        res[k++] = {Graph[i].from, Graph[i].to};
        cost += Graph[i].cost;
    }
    std::ofstream g("apm.out");
    g << cost << '\n' << res.size() << '\n';
    for (size_t i = 0; i < res.size(); ++i) {
        g << res[i].first << ' ' << res[i].second << '\n';
    }
    g.close();
    return 0;
}