Connect the Cities[HDU3371]
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]的更多相关文章
- Connect the Cities(hdu3371)并查集(附测试数据)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu3371 Connect the Cities (MST)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- hdoj 3371 Connect the Cities
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities(prime)
Connect the Cities Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
随机推荐
- Google Map API V3开发(3)
Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...
- Fast Member
https://github.com/mgravell/fast-member/ http://stackoverflow.com/questions/6158768/c-sharp-reflecti ...
- 关于Visual Studio 2015中没有报表项(ReportViewer)的解决方案。
没有报表,一般默认安装之后会出现这种情况,在安装的时候选择自定义安装,把Microsoft Office 开发人员工具.Microsoft SQL Server Data Tools勾选上,安装之后就 ...
- 【html】学习记录-拖放(drag and drop)
目的:实现拖动目标并放置到指定区域. 使元素可拖动,涉及到元素的全局属性draggable <img draggable="true" /> 即img元素设置为可拖 ...
- 解释序列号关键字的形象说法[Serializable]
举个栗子,你在qq中发送一段文字加图片混合的信息给某人的时候,qq不会提供多个方法分别接收文字和图片,而是将这段内容信息赋值给一个对象,然后序列化这个对象为 byte[],然后对这个数组编码,加密,压 ...
- 06OC之内存管理
在高级语言中,例如C#是通过垃圾回收机制(GC)来解决这个问题,但是在OC并没有类似的垃圾回收机制,因此必须由程序员手动去维护.今天就讲讲OC中的内存管理: 一.内存管理原理 在Xcode4.2之后的 ...
- Linux 架构
(转)作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! http://www.cnblogs.com/vamei/archive/2 ...
- 网址前面的icon
shortcut icon和icon代码之间究竟有何区别呢.下面介绍一下 语句一:<link rel="shortcut icon" href="favicon ...
- python写计算器
#!/usr/bin/env python # -*- coding:utf-8 -*- import re def chu(arg1): #定义加减 arg = arg1[0] #beacuse p ...
- 安装第三方模块方法和requests
如何安装第三方模块 pip3 pip3 install xxxx 源码 下载,解压 进入目录 python setup.py inst ...