炸力
查看原帖
炸力
352918
I_think_so_i_am_here楼主2024/3/31 10:09

二十分求调

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,w,sum,all,ff[100010];
int find(int x){
	if(ff[x]==x)return x;
	else return ff[x]=find(ff[x]);
}
bool f[100010];
struct node{
	int x,y,l;
}e[100010];
bool operator<(node a,node b){
	return a.l<b.l;
}
priority_queue<node>q;
signed main(){
	cin>>n>>k;
	for(int i=1;i<=k;i++)cin>>w,f[w]=true;
	for(int i=1;i<=n;i++)ff[i]=i;
	for(int i=1;i<n;i++){
		int xx,yy,ll;
		cin>>xx>>yy>>ll;
		q.push({xx,yy,ll});
		all+=ll;
	}
	while(!q.empty()){
		node ee=q.top();
		q.pop();
		if(f[ee.x]&&f[ee.y]&&find(ee.x)!=find(ee.y))continue;
		if(f[ee.x]||f[ee.y]){
			ff[find(ee.x)]!=find(ee.y);
			f[ee.x]=true;
			f[ee.y]=true;
		} 
		sum+=ee.l;
	}
	cout<<all-sum<<endl;
	return 0;
} 
2024/3/31 10:09
加载中...