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>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, m, q;
int parent[505];
vector<pair<pii, int> > edge;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= m; ++i) {
int a, b, w; cin >> a >> b >> w;
edge.push_back({{a, b}, w});
}
auto getParent = [&](auto&& self, int node) -> int {
if(parent[node] == node) return node;
else return parent[node] = self(self, parent[node]);
};
auto unionParent = [&](int u, int v) -> void {
u = getParent(getParent, u), v = getParent(getParent, v);
if(u > v) swap(u, v);
parent[v] = u;
};
cin >> q;
while(q--) {
int x; cin >> x;
priority_queue<pair<int, pii> > pq;
for(auto e : edge) pq.push({-abs(e.second - x), e.first});
for(int i = 1; i <= n; ++i) parent[i] = i;
ll ans = 0;
while(!pq.empty()) {
int c = -pq.top().first;
int a = pq.top().second.first, b = pq.top().second.second;
pq.pop();
if(getParent(getParent, a) == getParent(getParent, b)) continue;
ans += 1ll * c; unionParent(a, b);
}
cout << ans << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |