https://codeforces.com/contest/1095/problem/F

题解:并查集+最小生成树

TLE,WA,RE搞死中,就因为数组开小了,23333

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=410000;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,v,u;
ll  a[N],w;
int pre[N];
struct node{
    int u,v;
    ll w;
    node(){};
    node(int _u,int _v,ll _w){
        u=_u;
        v=_v;
        w=_w;
    }
    bool operator <(const node&S)const{
        return w<S.w;
    }
}e[N];
int find(int x){
    int rt=x;
    while(pre[rt]!=rt){
        rt=pre[rt];
    }
    pre[x]=rt;//并查集路径压缩
    return rt;
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%d%d",&n,&m);
    int id=1;
    for(int i=1;i<=n;i++){
        pre[i]=i;
        scanf("%I64d",&a[i]);
        if(a[i]<=a[id])id=i;
    }
    int tot=0;
    for(int i=1;i<=n;i++){
        //if(i!=id)
            e[++tot]=node(i,id,a[i]+a[id]);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d%I64d",&u,&v,&w);
        e[++tot]=node(u,v,w);
    }
    sort(e+1,e+tot+1);
    ll ans=0;
    for(int i=1;i<=tot;i++){
        int x=find(e[i].u);
        int y=find(e[i].v);
        if(x!=y){
            pre[x]=pre[y];
            ans+=e[i].w;
        }
    }
    cout << ans << endl;
    //cout << "Hello world!" << endl;
    return 0;
}