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 "dango3.h"
#include <bits/stdc++.h>
using namespace std;
int pr[501],gs[501];
int find(int x){
if(x==pr[x])return x;
return pr[x] = find(pr[x]);
}
bool mergegroup(int a,int b){
a = find(a);b = find(b);
if(a==b)return 0;
if(gs[a]<gs[b])swap(a,b);
pr[b] = a;
gs[a]+=gs[b];
return 1;
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("outout.txt","w",stdout);
int n,m;
cin>>n>>m;
vector<array<int,3>> ed;
for(int i = 0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
ed.push_back({a,b,c});
}
int q;
cin>>q;
while(q--){
int c;
cin>>c;
vector<array<int,3>> lol;
for(int i = 1;i<=n;i++){
pr[i] = i , gs[i] = 1;
}
for(int i = 0;i<m;i++){
lol.push_back({abs(ed[i][2]-c),ed[i][0],ed[i][1]});
}
sort(lol.begin(),lol.end());
long long all = 0;
for(auto i:lol){
if(mergegroup(i[1],i[2])){
all+=i[0];
}
}
cout<<all<<endl;
}
}
# | 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... |