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 ...
随机推荐
- 11月1日上午PHP批量删除
1.在主页面上添加批量删除有关代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- JavaScript 数组中的 indexOf 方法
let arr = ['orange', '2016', '2016']; arr.indexOf('orange'); //0 arr.indexOf('o'); //-1 arr.indexOf( ...
- thinkphp 3.2与phpexcel
thinkphp版本:3.2 1.在http://phpexcel.codeplex.com/下载最新PHPExcel 2.把Classes目录下的文件(PHPExcel.php和PHPExcel文件 ...
- EF 增删改
一.新增 UserInfo user = new UserInfo() { UserName = "jamsebing", UserPass = " }; db.User ...
- Discuzx系统 CSS 编码规范,CSS属性书写顺序
1. 属性写在一行内,属性之间.属性名和值之间以及属性与“{}”之间须有空格,例如:.class { width: 400px; height: 300px; } 2. 属性的书写顺序: ...
- Alpha版本十天冲刺——Day 10
站立式会议 最后一天,很高兴我们做出了跟预期差不多的版本,实现了基本功能,虽然还有一些bug,但是下一阶段我们会继续加油! 会议总结 队员 今天完成 遇到的问题 感想 鲍亮 功能细节更改 我的手机运行 ...
- mount -t nfs 的使用
服务安装:1. 在VMware Ubuntu中安装NFS服务: sudo apt-get install nfs-kernel-server2. 安装成功会出现配置文件/etc/exports. ls ...
- 利用js刷新页面方法
1,reload 方法,该方法强迫浏览器刷新当前页面. location.reload(force) 如果该方法没有规定参数,或者参数是 false,它就会用 HTTP 头 If-Modified-S ...
- MFC之进度条CProgressCtrl
一.成员函数简介 1.create()针对不是通过资源文件上拖拉进度条控件生成的进度条,需要用此函数创建一个. 2.SetRange()设置进度条的起始值和终止值. 3.SetPos()设置进度条的当 ...
- filedownload
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC "-/ ...