Cod sursa(job #3195190)

Utilizator AdrianRosuRosu Adrian Andrei AdrianRosu Data 20 ianuarie 2024 11:15:13
Problema Heavy Path Decomposition Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>
#define DIM 1000001
#define log yhgfrty
#define MOD 1000000007
#define int long long

using namespace std;

ifstream fin("rusuoaica.in");

ofstream fout("rusuoaica.out");

struct elem{

    int x, y, cost;

};

elem G[DIM];

int father[DIM], found[DIM];

int n, m, i, j, answer, a, x, nr;

int get_root(int node){

    if(!father[node])

        return node;

    int x = get_root(father[node]);

    father[node] = x;

    return x;

}

void join(int x, int y){

    father[x] = y;

}

bool cmp(const elem &a, const elem &b){

    return(a.cost < b.cost);

}

int32_t main(){

    fin >> n >> m >> a;

    for(i=1;i<=m;i++){

        fin >> G[i].x >> G[i].y >> G[i].cost;

        answer += G[i].cost;

    }

    sort(G + 1, G + m + 1, cmp);

    for(i=1;i<=m;i++){

        if(G[i].cost <= a){

            if(get_root(G[i].x) != get_root(G[i].y)){

                found[G[i].x] = 1;

                found[G[i].y] = 1;

                join(get_root(G[i].x), get_root(G[i].y));

                x += G[i].cost;

            }

        }

        else break;

    }

    for(i=1;i<=n;i++)

        if(!father[i])

            nr++;

    fout << x + (nr - 1) * a - answer + x;

}