PRIM
#include <iostream>
#include <string.h>
vis[1] = 1;
dis[1] = INF;}
并查集
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int x,y,val;
}w[25002*3];
int father[505];
bool cmp(const node &a,const node &b)
{
return a.val<b.val;
}
int find(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
int fun(int n,int m)
{
int ans,a,b;
ans=0;
for (int i=1;i<=m;i++)
{
a=find(w[i].x);
b=find(w[i].y);
if(a!=b)
{
father[a]=b;
ans+=w[i].val;
}
}
return ans;
}
int main()
{
int n,m,k,p,q,t,test,cnt;
scanf("%d",&test);
while(test--)
{
scanf("%d %d %d",&n,&m,&k);
for(int i=1;i<=n;i++)
father[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&w[i].x,&w[i].y,&w[i].val);
}
for(int i=0;i<k;i++)
{
scanf("%d %d",&t,&p);
for (int j=1;j<t;j++)
{
scanf("%d",&q);
father[find(q)]=father[find(p)];
}
}
sort(w+1,w+m+1,cmp);
if(n==1)
{
printf("0\n");
continue;
}
t=fun(n,m);
cnt=0;
for(int i=1;i<=n;i++)
if(father[i]==i)
cnt++;
if(cnt==1)
printf("%d\n",t);
else
printf("-1\n");
}
return 0;
}