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;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 505;
int P[N];
int n,m;
void reset(){
for(int i=1;i<=n;i++) P[i]=i;
}
int query(int x){
if(P[x]==x) return x;
return P[x]=query(P[x]);
}
struct edge{
int a,b,w;
edge(int _a,int _b,int _c){
a = _a;
b = _b;
w = _c;
}
};
vector<edge> ed;
void solve(int x){
vector<edge> h;
for(auto e : ed){
edge k = e;
k.w = abs(x-e.w);
h.push_back(k);
}
sort(h.begin(),h.end(),[](const edge &a,const edge &b){return a.w<b.w;});
reset();
ll ans = 0;
for(auto e : h){
if(query(e.a)!=query(e.b)){
ans+=e.w;
P[query(e.a)]=query(e.b);
}
}
cout<<ans<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b,c;cin>>a>>b>>c;
ed.push_back(edge(a,b,c));
}
int q;cin>>q;
while(q--){
int x;cin>>x;
solve(x);
}
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... |