http://codeforces.com/contest/1095/problem/F
求最小生成树。
如果没有那些特殊的边,那这个题就很明确了,找出最小的点,把其他的点连到这个点上去。
但是现在有特殊的边,那么就是这些特殊的边和上述的边在竞争,谁小连谁。
krustra跑一遍
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxN=2e5+5;
LL cost[maxN];
int id,N,M,tot;
int pre[maxN];
struct node
{
int s,t;
LL w;
}num[maxN<<1];
bool cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
return x==pre[x]?x:pre[x]=find(pre[x]);
}
LL krustra()
{
LL ans=0;
int cnt=0;
for(int i=1;i<=N;i++)pre[i]=i;
for(int i=1;i<=tot;i++)
{
int ss=find(num[i].s);
int tt=find(num[i].t);
if(ss==tt)continue;
pre[ss]=tt;
ans+=num[i].w;
if(++cnt==N)break;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin>>N>>M;
id=1;
for(int i=1;i<=N;i++)
{
cin>>cost[i];
id=cost[id]>cost[i]?i:id;
}
int u,v;
LL w;
while(M--)
{
cin>>u>>v>>w;
num[++tot]={u,v,w};
}
for(int i=1;i<=N;i++)
{
if(i==id)continue;
num[++tot]={i,id,cost[i]+cost[id]};
}
sort(num+1,num+1+tot,cmp);
cout<<krustra();
return 0;
}