Cod sursa(job #2620849)

Utilizator CristiPopaCristi Popa CristiPopa Data 29 mai 2020 19:22:37
Problema Arbore partial de cost minim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>

#define NMAX 200010

using namespace std;

int n , m, cost;
vector<tuple<int, int, int>> edges, apm;
int root[NMAX];

int get_set(int x) {
    while (x != root[x]) {
        x = root[x];
    }
    return x;
}

int main()
{
    cost = 0;
    ifstream fin("apm.in");
    fin >> n >> m;
    for (int i = 1, x, y, c; i <= m; ++i) {
        fin >> x >> y >> c;
        root[i] = i;
        edges.push_back(make_tuple(c, x, y));
    }
    fin.close();

    sort(edges.begin(), edges.end());
    for (auto &edge : edges) {
        int c = get<0>(edge), x = get<1>(edge), y = get<2>(edge);
        if (get_set(x) != get_set(y)) {
            cost += c;
            apm.push_back(edge);
            if (apm.size() == n - 1)
                break;
            root[get_set(x)] = get_set(y);
        }
    }

    ofstream fout("apm.out");
    fout << cost << endl << n - 1 << endl;
    for (auto &edge : apm) {
        fout << get<1>(edge) << ' ' << get<2>(edge) << endl;
    }
    fout.close();
    return 0;
}