Cod sursa(job #3380891)

Utilizator Andrei_tirTiru Andrei Vlad Andrei_tir Data 2 decembrie 2026 23:18:40
Problema Curcubeu Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>
using namespace std;

int p[100005];
int f(int x) {
    if (p[x] != x) p[x] = f(p[x]);
    return p[x];
}

int main() {
    freopen("rusuoaica.in", "r", stdin);
    freopen("rusuoaica.out", "w", stdout);
    int n, m, a;
    scanf("%d %d %d", &n, &m, &a);
    vector<array<int, 3>> e(m);
    long long total = 0;
    for (int i = 0; i < m; i++) {
        int x, y, c;
        scanf("%d %d %d", &x, &y, &c);
        e[i] = {c, x, y};
        total -= c;
    }
    sort(e.begin(), e.end());
    for (int i = 1; i <= n; i++) p[i] = i;
    int comp = n;
    for (auto &t : e) {
        if (t[0] > a) break;
        int x = f(t[1]), y = f(t[2]);
        if (x != y) {
            p[x] = y;
            comp--;
            total += 2 * t[0];
        }
    }
    total += (long long)a * (comp - 1);
    printf("%lld\n", total);
}