#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std; struct edge
{
int u,v,w;
}edges[]; bool cmp(const edge &a,const edge &b)
{
return a.w<b.w;
// a.w < b.w; //RE原因,忘记添加return
}
/*
bool cmp(edge a,edge b)
{
return a.w < b.w;
}*/
int pre[],vtc[];
void InitSet(int v)
{
for(int i=;i<=v+;i++)
pre[i] = i;
}
int find(int x)
{
if(x == pre[x]) return x;
return pre[x] = find(pre[x]); //路径压缩
//return find(pre[x]) //无路径压缩
}
bool UnionSet(int x,int y)
{
int fx = find(x);
int fy = find(y);
if(fx != fy)
{
pre[fy] = fx;
return false;
}
return true; //在一个集合中
}
int Kruscal(int e,int v)
{
int ans = ;
int cnt = ; for(int i=;i<=e&&cnt<v;i++)
{
bool k;
k = UnionSet(edges[i].u,edges[i].v); //不在同一集合才能加入,返回false
if(k == false)
{
//cout<<edges[i].u<<" "<<edges[i].v<<" "<<edges[i].w<<endl;
ans += edges[i].w;
cnt++;
}
}
return ans;
} int main()
{
int t;
cin>>t;
while(t--)
{
int v,e;
cin>>v>>e;
InitSet(v);
for(int i=;i<=e;i++)
{
scanf("%d %d %d",&edges[i].u,&edges[i].v,&edges[i].w);
}
sort(edges+,edges+e+,cmp);
int ans = Kruscal(e,v); for(int i=;i<v;i++)
scanf("%d",&vtc[i]);
sort(vtc,vtc+v);
cout<<ans + vtc[] <<endl;
}
return ;
}
05-11 11:36