楼间的最小生成树加外界的最小进入就行
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
typedef struct Tedge
{
int from, to;
int dist;
}Edge, * EEE;
Edge edge[250000];
int count;
int fa[600];
int find(int x)
{
if(x == fa[x])
return x;
fa[x]=find(fa[x]);
return(fa[x]);
}
int cmp(const void *a, const void *b)
{
return (*(EEE)a).dist > (*(EEE)b).dist ? 1 : -1;
}
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
int v, e;
int i;
scanf("%d%d", &v, &e);
for(i=0;i<e;i++)
scanf("%d%d%d", &edge[i].from, &edge[i].to, &edge[i].dist);
count = i;
int min = INT_MAX;
for(i=0;i<v;i++)
{
int a;
scanf("%d", &a);
if(a < min)
min = a;
}
qsort(edge, count, sizeof(edge[0]), cmp);
for(i=0;i<=v;i++)
fa[i] = i;
int num=0, sum=min;
for(i=0;i<count;i++)
{
if(find(edge[i].from) != find(edge[i].to))
{
fa[find(edge[i].from)] = find(edge[i].to);
sum += edge[i].dist;
num++;
if(num == v-1) break;
}
}
printf("%d\n", sum);
}
return 0;
}