2021年北京理工大学ACM CLUB清明节组队训练赛 G Generator Grid(并查集)

题目链接 https://ac.nowcoder.com/acm/contest/13924/G
题意 n个点有m个电塔,还有n条电线,问你都有电的最小费用
思路 并查集,将灯塔费用转化成0点与这个点之间电线费用,贪心做
代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long ull;
typedef long double lld;
#define lson p<<1
#define rson p<<1|1
const int N = 1e5+5;
const int seed = 26; 
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f; 
const double PI = acos(-1);
const double esp = 1e-6;
int n,m;
int f[N];
struct node{
	int u,v,w;
}a[N];
vector<node> v;
bool cmp(node a,node b){
	return a.w<b.w;
}
int find(int x){
	if(f[x]!=x) return f[x]=find(f[x]);
	return f[x];
}
ll qpow(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1) ans=ans*base;
		base=base*base;
		b>>=1;
	}
	return ans;
}
void solve(){
	ll res=0;
	scanf("%d %d",&n,&m);
	int x,y;
	for(int i=0;i<=m+n;i++) f[i]=i;
	for(int i=1;i<=m;i++){
		scanf("%d %d",&x,&y);
		v.push_back({0,x,y});
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		if(i!=n){
			v.push_back({i,i+1,x});
		}
		else{
			v.push_back({1,i,x});
		}
	}
	sort(v.begin(),v.end(),cmp);
	for(int i=0;i<v.size();i++){
		int fu=find(v[i].u);
		int fv=find(v[i].v);
		if(fu==fv) continue;
		res+=v[i].w;
		f[f[v[i].u]]=fv;
	}
	printf("%lld",res);
} 
int main() {
   	int Case=1;
	//scanf("%d",&Case); 
    while(Case--){
    	solve();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值