Submission #869666

#TimeUsernameProblemLanguageResultExecution timeMemory
869666willychanReconstruction Project (JOI22_reconstruction)C++14
7 / 100
5075 ms6928 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...