# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1061243 | thinknoexit | Reconstruction Project (JOI22_reconstruction) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct Edge {
int u, v, w;
bool operator < (const Edge& o) const {
return w < o.w;
}
} e[100100], e2[100100];
int p[505];
int fr(int i) {
return p[i] == i ? i : p[i] = fr(p[i]);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1;i <= m;i++) {
cin >> e[i].u >> e[i].v >> e[i].w;
}
sort(e + 1, e + 1 + m);
while (q--) {
int x;
cin >> x;
for (int i = 1;i <= m;i++) {
e2[i].u = e[i].u;
e2[i].v = e[i].v;
e2[i].w = abs(e[i].w - x);
}
for (int i = 1;i <= n;i++) p[i] = i;
sort(e2 + 1, e2 + 1 + m);
ll ans = 0;
for (int i = 1;i <= m;i++) {
int pu = fr(e2[i].u), pv = fr(e2[i].v);
if (pu == pv) continue;
ans += e2[i].w;
p[pu] = pv;
}
cout << ans << '\n';
}
return 0;
}