Connect the Cities

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

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

kruskal会超时,要用prim。朴素的prim也是极限时间过的,最好加个堆优化。

  1. #include <stdio.h>
  2. #include <string.h>
  3. using namespace std;
  4. class Prim {
  5. #define Prim_MAXN 505
  6. #define Prim_MAXM 100005
  7. public:
  8. int N, M;
  9. int head[Prim_MAXN], dis[Prim_MAXN];
  10. bool vis[Prim_MAXN];
  11. struct EDGE {
  12. int v, d, nex;
  13. } edge[Prim_MAXM];
  14. Prim() {
  15. clear();
  16. }
  17. void clear() {
  18. N = M = ;
  19. memset(head, -, sizeof(head));
  20. }
  21. void addEdge(int a, int b, int c) {
  22. edge[M].v = b;
  23. edge[M].d = c;
  24. edge[M].nex = head[a];
  25. head[a] = M++;
  26. edge[M].v = a;
  27. edge[M].d = c;
  28. edge[M].nex = head[b];
  29. head[b] = M++;
  30. }
  31. int MST() {
  32. int ret = , last, next, min;
  33. for (int i = ; i <= N; i++) {
  34. dis[i] = 0x7FFFFFFF;
  35. }
  36. memset(vis, false, sizeof(vis));
  37. vis[] = true;
  38. last = ;
  39. for (int i = ; i < N; i++) {
  40. for (int e = head[last]; e != -; e = edge[e].nex) {
  41. if (dis[edge[e].v] > edge[e].d) {
  42. dis[edge[e].v] = edge[e].d;
  43. }
  44. }
  45. min = 0x7FFFFFFF;
  46. for (int j = ; j <= N; j++) {
  47. if (dis[j] < min && !vis[j]) {
  48. min = dis[j];
  49. next = j;
  50. }
  51. }
  52. if (min == 0x7FFFFFFF) {
  53. return -;
  54. }
  55. vis[next] = true;
  56. ret += dis[next];
  57. last = next;
  58. }
  59. return ret;
  60. }
  61. };
  62. Prim Pr;
  63. int main() {
  64. int n, m, t, k;
  65. scanf("%d", &t);
  66. while (t--) {
  67. Pr.clear();
  68. scanf("%d%d%d", &n, &m, &k);
  69. Pr.N = n;
  70. for (int i = ; i < m; i++) {
  71. int a, b, c;
  72. scanf("%d%d%d", &a, &b, &c);
  73. Pr.addEdge(a, b, c);
  74. }
  75. int nn, x[];
  76. for (int i = ; i < k; i++) {
  77. scanf("%d", &nn);
  78. for (int j = ; j < nn; j++) {
  79. scanf("%d", &x[j]);
  80. }
  81. for (int j = ; j < nn; j++) {
  82. Pr.addEdge(x[], x[j], );
  83. }
  84. }
  85. printf("%d\n", Pr.MST());
  86. }
  87. return ;
  88. }

Connect the Cities[HDU3371]的更多相关文章

  1. Connect the Cities(hdu3371)并查集(附测试数据)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. hdu3371 Connect the Cities (MST)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. Connect the Cities(MST prim)

    Connect the Cities Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

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

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

  5. hdu 3371 Connect the Cities

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

  6. hdoj 3371 Connect the Cities

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. Connect the Cities(prime)

    Connect the Cities Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  8. Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

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

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

随机推荐

  1. WinPcap4.13无法安装解决方法

    360软件管家提示把WinPcap更新至版本:4.1.0.2980,于是把旧版下载后,可新版本怎么也无法顺利安装,出现以下信息,旧版本已安装,关闭所有winpcap-based应用程序和再次运行安装程 ...

  2. UITabBarController 、TabBar背景颜色设置,UITabBarItem的文字样式(颜色和大小)UITabBarItem的位置调整

    改变UITabBarController的颜色 UIView*mView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,48)];//这是部分tabb ...

  3. SQLPROMPT5.3对各种加密对象的解密测试

    SQLPROMPT5.3对各种加密对象的解密测试 测试环境: SQL2005个人开发者版 SP4 SQLPROMPT版本:5.3.8.2 视图 CREATE VIEW aa WITH ENCRYPTI ...

  4. 深入理解javascript原型和闭包(3)——prototype原型

    既typeof之后的另一位老朋友! prototype也是我们的老朋友,即使不了解的人,也应该都听过它的大名.如果它还是您的新朋友,我估计您也是javascript的新朋友. 在咱们的第一节(深入理解 ...

  5. Another app is currently holding the yum lock

    摘要 在使用yum安装的时候,出现该error. 错误 Another app is currently holding the yum lock; waiting for it to exit... ...

  6. MRDS学习二——机械车

    准备机械车: 第一步:从Service中选择一个Generic Differential Drive (通用差速驱动:同一轴的左右轮胎可以转动不同速度的车子)放入Diagram中. 第二步:对其进行配 ...

  7. 转】C#接口-显式接口和隐式接口的实现

    [转]C#接口-显式接口和隐式接口的实现 C#中对于接口的实现方式有隐式接口和显式接口两种: 类和接口都能调用到,事实上这就是“隐式接口实现”. 那么“显示接口实现”是神马模样呢? interface ...

  8. 今天在学习NTP时发现了2个网站

    NTP 调整系统时间 一个网站是:http://chrony.tuxfamily.org/doc/1.31/manual.html  这个是专门介绍chrony的,做的很详细. 另外一个是:http: ...

  9. mysql的DISABLE/ENABLE KEYS

    有一个表 tbl1 的结构如下: CREATE TABLE `tbl1` ( `id` int(10) unsigned NOT NULL auto_increment, `name` char(20 ...

  10. BitMap算法应用:Redis队列滤重优化

    工作中有用到Redis滤重队列. 原来的方法如下: 方法一 为了保证操作原子性,使用Redis执行Lua脚本. 在脚本中的逻辑是,如果队列不超过某个数值,进行一次lrem操作(队列使用list结构), ...