题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10313    Accepted Submission(s):
2937

Problem Description
In 2100, since the sea level rise, most of the cities
disappear. Though some survived cities are still connected with others, but most
of them become disconnected. The government wants to build some roads to connect
all of these cities again, but they don’t want to take too much money.  
 
Input
The first line contains the number of test
cases.
Each test case starts with three integers: n, m and k. n (3 <= n
<=500) stands for the number of survived cities, m (0 <= m <= 25000)
stands for the number of roads you can choose to connect the cities and k (0
<= k <= 100) stands for the number of still connected cities.
To make
it easy, the cities are signed from 1 to n.
Then follow m lines, each
contains three integers p, q and c (0 <= c <= 1000), means it takes c to
connect p and q.
Then follow k lines, each line starts with an integer t (2
<= t <= n) stands for the number of this connected cities. Then t integers
follow stands for the id of these cities.
 
Output
For each case, output the least money you need to take,
if it’s impossible, just output -1.
 
Sample Input
1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6
 
Sample Output
1
 
题目大意:
这一题也就是一个很简单的prim算法的变形,题意很容易理解,大概是这样的:一场大雨摧毁可很多路,但是还有一些依然存留,所以题目给出了一些路,还给了已经建好的一些,目的是求所有的路全部连通的最小花费。
在比赛的时候一直想用克鲁斯卡尔,结果不知道是哪里出问题了,一直不能ac,所有。。。。依旧是差这一题ak,在这里反思一下,应该学会转变,不能一直一个思路走下去,结果比赛后,看了这个题目,用prim很容易的解决了,只要在建好路的地图map[a][b]=map[b][a]=0就ok了!还有要注意判重~
 
详见代码。
 
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. int map[][],r[],Min,n,sum,node[];
  6. const int INF=;
  7.  
  8. void set()
  9. {
  10. for (int i=; i<=n; i++)
  11. {
  12. node[i]=INF;
  13. for (int j=; j<=n; j++)
  14. map[i][j]=INF;
  15. }
  16. }
  17.  
  18. void prim()
  19. {
  20. int tm=,m;
  21. int vis[]= {};
  22. node[tm]=;
  23. vis[tm]=;
  24. for (int k=; k<=n; k++)
  25. {
  26. Min=INF;
  27. for (int i=; i<=n; i++)
  28. if (!vis[i])
  29. {
  30. if (node[i]>map[tm][i])
  31. node[i]=map[tm][i];
  32. if (Min>node[i])
  33. {
  34. Min=node[i];
  35. m=i;
  36. }
  37. }
  38. tm=m;
  39. sum+=Min;
  40. vis[m]=;
  41. }
  42. }
  43.  
  44. int main ()
  45. {
  46. int T;
  47. cin>>T;
  48. while (T--)
  49. {
  50. sum=;
  51. int m,k;
  52. scanf("%d%d%d",&n,&m,&k);
  53. set();
  54. for (int i=; i<=m; i++)
  55. {
  56. int p,q,c;
  57. scanf("%d%d%d",&p,&q,&c);
  58. if (map[p][q]>c)
  59. map[p][q]=map[q][p]=c;
  60. }
  61. while (k--)
  62. {
  63. int t;
  64. scanf("%d",&t);
  65. for (int i=; i<=t; i++)
  66. {
  67. cin>>r[i];
  68. }
  69. for (int i=; i<=t; i++)
  70. {
  71. for (int j=; j<=t; j++)
  72. {
  73. map[r[i]][r[j]]=map[r[j]][r[i]]=;
  74. }
  75. }
  76. }
  77. prim();
  78. if (sum<INF)
  79. printf ("%d\n",sum);
  80. else
  81. printf ("-1\n");
  82. }
  83. return ;
  84. }

hdu 3371(prim算法)的更多相关文章

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

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

  2. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  3. hdu 1162 Eddy&#39;s picture (Kruskal算法,prim算法,最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法.保证联通的线路最短,典型的最小生成 ...

  4. MST(最小生成树)——Prim算法——HDU 1879-继续畅通工程

    Prim算法很好理解,特别是学完了迪杰斯特拉算法之后,更加能理解Prim的算法思想 和迪杰斯特拉算法差不多,由于最后要形成连通图,故任意指定一个点,作为初始点,遍历所有点,以当前最小权值的点(和迪杰斯 ...

  5. hdu 1233 还是畅通工程 最小生成树(prim算法 + kruskal算法)

    还是畅通工程                                                                            Time Limit: 4000/2 ...

  6. HDU1102 最小生成树prim算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:给出任意两个城市之间建一条路的时间,给出哪些城市之间已经建好,问最少还要多少时间使所有的城 ...

  7. 1.1.2最小生成树(Kruskal和Prim算法)

    部分内容摘自 勿在浮沙筑高台 http://blog.csdn.net/luoshixian099/article/details/51908175 关于图的几个概念定义: 连通图:在无向图中,若任意 ...

  8. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  9. 最小生成树のprim算法

    Problem A Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

随机推荐

  1. Node js MongoDB简单操作

    //win7环境下node要先安装MongoDB的相关组件(非安装MongoDB数据库),在cmd命令行进入node项目目录后执行以下语句 //npm install mongodb //创建连接 v ...

  2. C# 使用this的形参

    示例1: public static RectangleF TransformRect(this Matrix mat, RectangleF rect) 是向Matrix类扩展带有Rectangle ...

  3. Ubuntu编译内核树

    什么是内核树?刚开始我也没弄明白,通过这几天的学习,有所感悟,就说说我的理解吧!从形式上看,内核树与内核源码的目录结构形式是相同的,都是由各个层次的文件目录结构组成,但是其中的具体内容肯定是不同的.从 ...

  4. 【Asp.Net Core】ASP.NET Core 2.0 + EF6 + Linux +MySql混搭

    好消息!特好消息!同时使用ASP.NET Core 2.0和.NET Framework类库还能运行在linux上的方法来啦! 是的,你没有看错!ASP.NET Core 2.0,.NET Frame ...

  5. RT-thread 设备驱动组件之PIN设备

    在RT-thread 2.0.0正式版中引入了pin设备作为杂类设备,其设备驱动文件pin.c在rt-thread-2.0.1\components\drivers\misc中,主要用于操作芯片GPI ...

  6. 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT

    题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...

  7. Vika and Segments - CF610D

    Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-d ...

  8. [CQOI2009]跳舞 网络流

    题面:[CQOI2009]跳舞 题解: 首先最大时间不好求,而且数据范围很小,所以我们可以先二分一个最大时间,然后就只需要判断是否可行即可. 因此我们每二分一个mid,对于每个女生,连s ---> ...

  9. POJ1816:Wild Words——题解

    http://poj.org/problem?id=1816 比较麻烦的trie. 首先你需要选择针对n还是m建立trie,这里我选择了针对n. 那么就需要面临卡空间的问题. 这里提供了一种链式前向星 ...

  10. 洛谷 P4735 最大异或和 解题报告

    P4735 最大异或和 题目描述 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的 ...