Cod sursa(job #2981174)

Utilizator cberindeCodrin Berinde cberinde Data 17 februarie 2023 14:25:01
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fi("radiatie.in");
ofstream fo("radiatie.out");

int N, M, K;
long long p[30001], rnk[30001], sol[30001];

struct nod {
    int nume;
    map<int, long long> qry;
} lor[500001];

struct muchie {
    int a, b;
    long long c;
};

bool operator < (muchie x, muchie y){
    return x.c > y.c;
}

priority_queue<muchie> q;

void citire() {
    fi >> N >> M >> K;
    muchie x;
    for(int i = 0; i < M; i++) {
        fi >> x.a >> x.b >> x.c;
        q.push(x);
    }
    for(int i = 0; i < K; i++) {
        int nd;
        for(int j = 0; j < 2; j++) {
            fi >> nd;
            lor[nd].qry.insert({i, 1});
        }
    }
}

int parinte(int nod) {
    if(p[nod] == 0)
        return nod;
    p[nod] = parinte(p[nod]);
    return p[nod];
}

void kruskal() {
    while(!q.empty()) {
        muchie x = q.top();
        q.pop();
        int aa, bb;
        aa = parinte(x.a);
        bb = parinte(x.b);
        if(aa != bb) {
            if(rnk[aa] < rnk[bb])
                swap(aa, bb);
            p[bb] = aa;
            rnk[aa] += rnk[bb] + 1;

            for(auto u: lor[bb].qry) {
                if(lor[aa].qry[u.first] != 0) {
                    sol[u.first] += u.second * lor[aa].qry[u.first] * x.c;
                }
                lor[aa].qry[u.first] += u.second;
            }
        }
    }
}

void afisare() {
    for(int i = 0; i < K; i++) {
        fo << sol[i] << "\n";
    }
}

int main() {
    citire();
    kruskal();
    afisare();
    return 0;
}