Cod sursa(job #2278503)

Utilizator bogdanmarin69Bogdan Marin bogdanmarin69 Data 8 noiembrie 2018 09:29:17
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
const int MAX = 2e5 + 1, DELTA = 1000;
int n, m, tata[MAX], cost;
vector<pair<int, int> > lst[2 * DELTA + 1], sol;
int find_tata(int x) {
    if(tata[x] == x) {
        return x;
    }
    int ans = find_tata(tata[x]);
    tata[x] = ans;
    return ans;
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        lst[w + DELTA].push_back({u, v});
    }
    for(int i = 1; i <= n; ++i) {
        tata[i] = i;
    }
    for(int i = 0; i <= 2 * DELTA; ++i) {
        for(auto it : lst[i]) {
            int tu = find_tata(it.first);
            int tv = find_tata(it.second);
            if(tu != tv) {
                cost = cost + i - DELTA;
                sol.push_back(it);
                tata[tu] = tv;
            }
        }
    }
    cout << cost << '\n' << n - 1 << '\n';
    for(auto it : sol) {
        cout << it.first << ' ' << it.second << '\n';
    }
    return 0;
}