2018年10月17日提高组 T1 平津战役

大意

给定 n n n个节点,现在要删除总代价最小的边使得指定的 m m m个点两两不连通


思路

不考虑拆边,考虑建边

因为我们要使删除总代价尽量少,也就是保留的边尽量大,也就是生成树,排序+并查集即可


代码

#include<cstdio>
#include<algorithm>
#define N 100001
using namespace std;long long ans;
int n,k,f[N],a,b;
bool vis[N];
inline int fa(register int x){return x==f[x]?x:f[x]=fa(f[x]);}//并查集
struct node{int from,to,w;}e[N];
inline bool cmp(node x,node y){return x.w>y.w;}
inline int read()//快读
{
	char c;int d=1,f=0;
	while(c=getchar(),c<48||c>57)if(c=='-')d=-1;f=(f<<3)+(f<<1)+c-48;
	while(c=getchar(),c>47&&c<58)f=(f<<3)+(f<<1)+c-48;
	return d*f;
}
signed main()
{
	n=read();k=read();f[n]=n;
	for(register int i=1;i<=k;i++) vis[read()]=1;
	for(register int i=1;i<n;i++) e[i]=(node){read(),read(),read()},f[i]=i,ans+=e[i].w;
	sort(e+1,e+n,cmp);
	for(register int i=1;i<n;i++)
	{
		a=fa(e[i].from);b=fa(e[i].to);
		if(vis[a]&vis[b]) continue;//若在此之前已经连接了则不管它
		f[a]=f[b];
		ans-=e[i].w;
		vis[a]=vis[b]=vis[a]|vis[b];//连接
	}
	printf("%lld",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值