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. __index

    Window = {} Window.prototype = {x = , y = , width = , height = } Window.mt = {} function Window.new( ...

  2. 【ESRI论坛6周年征文】ArcEngine注记(Anno/ Label/Element等)处理专题 -入门篇

    原发表于ESRI中国社区,转过来.我的社区帐号:jhlong http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=122097 ----------- ...

  3. linux下apache各种跳转(包括伪静态)的配置

      1.404跳转: vi /etc/httpd/conf/httpd.conf 在虚拟主机配置里添加一行:ErrorDocument 404 /404.html 2.301跳转: 1)将不带www的 ...

  4. mysql优化记录

    老板反应项目的反应越来越慢,叫优化一下,顺便学习总结一下mysql优化. 不同引擎的优化,myisam读的效果好,写的效率差,使用场景 非事务型应用只读类应用空间类应用 Innodb的特性,innod ...

  5. HSF和Dubbo有什么区别

    一. 以下摘录自企业级分布式应用服务EDAS官网段落 RPC服务 提供对Dubbo和HSF两个RPC框架的支持.阿里巴巴第一代RPC框架Dubbo是国内第一款成熟的商用级RPC框架,已于2011年正式 ...

  6. MVC验证session是否过期,在每个action执行之前验证

            protected override void OnActionExecuting(ActionExecutingContext filterContext)         {   ...

  7. 还原MySql数据库失败:max_allowed_packet 设置过小导致记录写入失败

    MySQL根据配置文件会限制Server接受的数据包大小. 有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置 show VARIABLES ...

  8. 利用Nginx实现域名转发 不修改主机头

    在conf下 新建一个 文件 格式 : 域名.conf  例如:www.test.com.conf 文件里配置: server{ listen 80; server_name www.test.com ...

  9. Linux学习之二--搭建FTP服务器

    一.查看是否安装有FTP rpm -qa|grep vsftpd 二.如果没有安装,就安装FTP yum install -y vsftpd 三.加入开机启动 systemctl enable vsf ...

  10. 知识联结梳理 : I/O多路复用、EPOLL(SELECT/POLL)、NIO、Event-driven、Reactor模式

    为了形成一个完整清晰的认识,将概念和关系梳理出来,把坑填平. I/O多路复用 I/O多路复用主要解决传统I/O单线程阻塞的问题.它通过单线程管理多个FD,当监听的FD有状态变化的时候的,调用回调函数, ...