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也是极限时间过的,最好加个堆优化。

#include <stdio.h>
#include <string.h>
using namespace std;
class Prim {
#define Prim_MAXN 505
#define Prim_MAXM 100005
public:
int N, M;
int head[Prim_MAXN], dis[Prim_MAXN];
bool vis[Prim_MAXN];
struct EDGE {
int v, d, nex;
} edge[Prim_MAXM];
Prim() {
clear();
}
void clear() {
N = M = ;
memset(head, -, sizeof(head));
}
void addEdge(int a, int b, int c) {
edge[M].v = b;
edge[M].d = c;
edge[M].nex = head[a];
head[a] = M++;
edge[M].v = a;
edge[M].d = c;
edge[M].nex = head[b];
head[b] = M++;
}
int MST() {
int ret = , last, next, min;
for (int i = ; i <= N; i++) {
dis[i] = 0x7FFFFFFF;
}
memset(vis, false, sizeof(vis));
vis[] = true;
last = ;
for (int i = ; i < N; i++) {
for (int e = head[last]; e != -; e = edge[e].nex) {
if (dis[edge[e].v] > edge[e].d) {
dis[edge[e].v] = edge[e].d;
}
}
min = 0x7FFFFFFF;
for (int j = ; j <= N; j++) {
if (dis[j] < min && !vis[j]) {
min = dis[j];
next = j;
}
}
if (min == 0x7FFFFFFF) {
return -;
}
vis[next] = true;
ret += dis[next];
last = next;
}
return ret;
}
};
Prim Pr;
int main() {
int n, m, t, k;
scanf("%d", &t);
while (t--) {
Pr.clear();
scanf("%d%d%d", &n, &m, &k);
Pr.N = n;
for (int i = ; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
Pr.addEdge(a, b, c);
}
int nn, x[];
for (int i = ; i < k; i++) {
scanf("%d", &nn);
for (int j = ; j < nn; j++) {
scanf("%d", &x[j]);
}
for (int j = ; j < nn; j++) {
Pr.addEdge(x[], x[j], );
}
}
printf("%d\n", Pr.MST());
}
return ;
}

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. jvm垃圾回收机制

    http://blog.csdn.net/zsuguangh/article/details/6429592 原文地址

  2. 漫谈JVM

    背景介绍 JVM已经是Java开发的必备技能了,JVM相当于Java的操作系统. JVM,java virtual machine, 即Java虚拟机,是运行java class文件的程序. Java ...

  3. 创建文本注记TextElement

    1.创建一个字体 /// <summary> /// 字体设置 /// </summary> /// <param name="size">Th ...

  4. Linux解压,压缩小总结

    linux下打包与解压的三种命令 最近在读<鸟歌的Linux私房菜基础篇>,想着总结一下所读知识,有益于理解. Linux下常用的命令有三种 gzip,zcat(用于zip,gzip等) ...

  5. Java排序算法——桶排序

    文字部分为转载:http://hxraid.iteye.com/blog/647759 对N个关键字进行桶排序的时间复杂度分为两个部分: (1) 循环计算每个关键字的桶映射函数,这个时间复杂度是O(N ...

  6. python基础知识

    由于python的灵活性,赋值前无需强调变量的数据类型,并且变量的数据类型在后期的操作过程中还可以改变,故不介绍关键字,直接定义方法及可以调用的方法. I  基本数据类型 一.字符串 1.使用单引号或 ...

  7. ubuntu12.04server下red5-1.0.0RC1的部署

    一.搭建环境 Linux版本:ubuntu12.04sever  64位 Java  版本:Java 1.7(jdk+jre) Red5 版本:red5-1.0.0-RC1 二.安装JDK 下载jdk ...

  8. ASCII码表

    ASCII码表 ASCII码大致可以分作三部分組成. 第一部分是:ASCII码非打印控制字符: 第二部分是:ASCII码打印字符: 第三部分是:扩展ASCII码打印字符. 第一部分:ASCII非打印控 ...

  9. Asp.Net Core--基于声明的授权

    翻译如下: 当创建身份时,其可以被分配由可信方发布的一个或多个声明. 索赔是名称值对,表示主题是什么,而不是主体可以做什么. 例如,您可能有驾驶执照,由当地驾驶执照颁发. 您的驾驶执照上有您的出生日期 ...

  10. error 502 in ngin php5-fpm

    unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) http://stackoverflow.com/q ...