BZOJ 1232 Kruskal

思路:
跟昨天的考试题特别像…..

就是裸的Kruskal 把边权设为连接的两个点的点权之和加上边权*2

搞定

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,c[10005],f[10005],minn=0x3fffffff,ans;
struct Node{int from,to,weight;}node[100050];
bool cmp(Node a,Node b){return a.weight<b.weight;}
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&c[i]),f[i]=i,minn=min(minn,c[i]);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&node[i].from,&node[i].to,&node[i].weight);
        node[i].weight=node[i].weight*2+c[node[i].from]+c[node[i].to];
    }
    sort(node+1,node+1+m,cmp);
    for(int i=1;i<=m;i++){
        int fx=find(node[i].from),fy=find(node[i].to);
        if(fx!=fy){
            f[fx]=fy;
            ans+=node[i].weight;
        }
    }
    printf("%d\n",ans+minn);
}

这里写图片描述

posted @ 2016-10-27 15:58  SiriusRen  阅读(128)  评论(0编辑  收藏  举报