#include<stdio.h>

#include<stdlib.h>

#define N 501

struct node {

int x,y,dis;

}road[N*N];

int pre[N];

int find (int n ) {

return pre[n]=n==pre[n]?n:find(pre[n]);

}

int cmp(const void *a,const void *b) {

return (*(struct node *)a).dis>(*(struct node *)b).dis?1:-1;

}

int main() {

       int n,m,k,t,a,b,c,cnt,sum,flag,i,j,len,f1,f2;

   scanf("%d",&t);

   while(t--) {

  scanf("%d%d%d",&n,&m,&k);

  for(i=1;i<=n;i++)

  pre[i]=i;

  for(i=0;i<m;i++) {

  scanf("%d%d%d",&a,&b,&c);

  road[i].x=a;

  road[i].y=b;

  road[i].dis=c;

  }

  sum=0;

  cnt=0;

  qsort(road,m,sizeof(road[0]),cmp);

  while(k--) {

  scanf("%d%d",&len,&a);

  f1=find(a);

  len--;

  while(len--) {

  scanf("%d",&b);

  f2=find(b);

  if(f1!=f2) {

  cnt++;

 

  pre[f2]=f1;

  }

  }

  }

  for(i=0;cnt<n&&i<m;i++) {

  f1=find(road[i].x);

  f2=find(road[i].y);

  if(f1!=f2) {

  cnt++;

  sum+=road[i].dis;

  pre[f2]=f1;

  }

  }

  if(cnt==n-1)

  printf("%d\n",sum);

  else

  printf("-1\n");

  }

  return 0;

   }

hdu 3371的更多相关文章

  1. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  2. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  3. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  4. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  5. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  6. hdu 3371(prim算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Jav ...

  7. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  8. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  9. HDU 3371(城市联通 最小生成树-Kruskal)

    题意是求将所有点联通所花费的最小金额,如不能完全联通,输出 -1 直接Kruskal,本题带来的一点教训是 rank 是algorithm头文件里的,直接做变量名会导致编译错误.没查到 rank 的具 ...

  10. HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

随机推荐

  1. JavaWeb ,EL,

    WEB 概述: java web 是用java 技术来解决相关web 互联网领域的技术总和 . web 可分为 web服务器和web客户端(浏览器)    web 的资源分类:  静态资源:  HTM ...

  2. JDK NIO SelectionKey bug

    此bug项目中使用elasticSearch中出现的,原因是,nio事件选择器,在特性内核下以及jdk6版本中,出现不hold线程,死循环获取事件的bug,导致cup使用率过高: 此bug在官网已被修 ...

  3. Android开发精品收藏贴

    各种下拉刷新效果: https://github.com/scwang90/SmartRefreshLayout

  4. SpringBoot自定义一个starter

    @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigureAfter //指定自动配置类的顺序 ...

  5. vue之组件的使用(转载)

    在工程目录/src下的component文件夹下创建一个 firstcomponent.vue并写仿照 App.vue 的格式和前面学到的知识写一个组件. <template> <d ...

  6. 检查bug

    用selective_search生成的坐标是(ymin,xmin,ymax,xmax),并且是从1开始的,不是从0 这是cache中的gt数据,明显看到有65535,说明很有可能是0-1变成了655 ...

  7. python小随笔

    关于pip安装 .\pip.exe install --ignore-installed --upgrade tensorflow-gpu python可视化库 Seaborn:是一个基于matplo ...

  8. CNN完成mnist数据集手写数字识别

    # coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data d ...

  9. MATLAB读取每个文件夹下的badcsv文件后合并为总的badexcel文件

    clear; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%读取子文件夹中bad.csv数据%并把所有数据写到一个excel文件中%%%%%%%%%%%%%%%%%% ...

  10. tmp note

    cat file.txt > /dev/null 2>&1 丢弃错误和标准输出 systemctl isolate multi-user.target 切换回多用户命令行模式 sy ...