蓝桥杯 安慰奶牛

简单 最小生成树 将变转化为 Li=2*Li+Si+EI 因为那个人还要走回来 这样变化就能够计算出 来回要花费的时间  然后进行一次最小生成树

#include <iostream>
#include <string.h>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=10005;
const int maxm=100005;
struct edge{
 int x,y;
 long long W;
}E[maxm];
int F[maxn];
long long N[maxn],maxv;
int father(int a){
   return a==F[a]?a:F[a]=father(F[a]);
}
bool cmp(const edge A,const edge B){
   return A.W<=B.W;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2){
            maxv=10000000;
        for(int i=1;i<=n;i++) F[i]=i;
        for(int i=1;i<=n;i++){
            cin>>N[i]; maxv=maxv>N[i]?N[i]:maxv;
        }
        for(int i=0;i<m;i++){
            cin>>E[i].x>>E[i].y>>E[i].W;
            E[i].W=E[i].W*2+N[E[i].x]+N[E[i].y];
        }
         sort(E,E+m,cmp);
         int L=n;
         for(int i=0;i<m;i++){
            int u=father(E[i].x),v=father(E[i].y);
            if(u!=v){ L--; maxv+=E[i].W;F[u]=v;}
            if(L==1) break;
         }
         cout<<maxv<<endl;
    }

    return 0;
}


 

原文地址:https://www.cnblogs.com/Opaser/p/3662038.html