题目链接 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;
}