超级传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=3371
后面的先不写,等待写个Prim比较。。。
#include<iostream>
#include<queue>
using namespace std;
const int MAX_V = 510;
const int MAX_E = 25020;
const int inf = 900000000;
int n,m,k;
struct Edge
{
int v1;
int v2;
int w;
Edge(int v1_ = 0,int v2_ = 0,int w_ = inf)
{
v1 = v1_;
v2 = v2_;
w = w_;
}
}edge[MAX_E];
int cmp(const void *a,const void *b)
{
Edge c = *(Edge *)a;
Edge d = *(Edge *)b;
return c.w - d.w;
}
//并查集的函数,数组
int father[MAX_V];
int rank[MAX_V];
void Init()
{
int i;
for(i=0;i<MAX_V;i++)
{
father[i] = i;
rank[i] = 1;
}
}
int find(int x)
{
if(father[x] != x)
{
father[x] = find(father[x]);
}
return father[x];
}
void uni(int a,int b)
{
int x = find(a);
int y = find(b);
if(x != y)
{
father[y] = father[x];
rank[x] += rank[y];
}
}
int Kruskal()
{
int sum = 0;
int i;
Edge temp;
qsort(edge,m,sizeof(edge[0]),cmp);
for(i=0;i<m;i++)
{
temp = edge[i];
if(find(temp.v1) != find(temp.v2))
{
sum += temp.w;
uni(temp.v1,temp.v2);
}
}
return sum;
}
int main()
{
int T;
int i,j;
int t;
int p[510];
int ans;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d",&n,&m,&k);
Init();
for(i=0;i<m;i++)
{
scanf("%d %d %d",&edge[i].v1,&edge[i].v2,&edge[i].w);
}
for(i=0;i<k;i++)
{
scanf("%d",&t);
for(j=0;j<t;j++)
{
scanf("%d",&p[j]);
}
for(j=1;j<t;j++)
{
uni(p[0],p[j]);
}
}
ans = Kruskal();
if(rank[find(1)] != n)
{
printf("-1\n");
}
else
{
printf("%d\n",ans);
}
}
return 0;
}