Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的
Connect the Cities |
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
| Total Submission(s): 701 Accepted Submission(s): 212 |
|
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 |
|
Sample Output
1 |
|
Author
dandelion
|
|
Source
HDOJ Monthly Contest – 2010.04.04
|
|
Recommend
lcy
|
/*
点多变少的时候用克利斯卡尔算法,点少边的时候用prim算法 真心的心累,交了20几发卡时过的
*/
#include<bits/stdc++.h>
#define N 550
#define INF 0x3f3f3f3f
using namespace std;
/*********输入输出外挂************/
template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF)
return ; //EOF
while(c!='-'&&(c<''||c>''))
c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='')
ret=ret*+(c-'');
ret*=sgn;
return ;
}
inline void out(int x)
{
if(x>)
out(x/);
putchar(x%+'');
}
/*********输入输出外挂************/
int t;
int n,m,k;
int mapn[][];//用来构建图
bool vis[];//用来记录那个点访问过也就是判环用的
int d[];//表示选中的结点到当前结点的最小距离
int x,y,val,q,rt[];
void init()
{
memset(mapn,INF,sizeof mapn);
memset(vis,false,sizeof vis);
memset(d,INF,sizeof d);
}
int prim(int m)
{
int root;//表示当前选中的结点
int minn=INF;
root=m;
vis[m]=true;
long long cur=;
int len=;
for(int i=;i<=n;i++)//优化的prim算法,总共要找n个点嘛,先找了m了,然后再找n-1个点就够了
{
minn=INF;
for(int j=;j<=n;j++)//然后开始找点
if(!vis[j])//这个点没有遍历过
{
if(d[j]>mapn[root][j])
d[j]=mapn[root][j];
if(minn>d[j])
{
minn=d[j]; m=j;
}
}
if(!vis[m])//这个点没有遍历过
{
cur+=minn;
vis[m]=true;
root=m;
len++; }
}
if(len==n-)
return cur;
return -;
}
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
scan_d(t);
while(t--)
{
scan_d(n);scan_d(m);scan_d(k);
init();//初始化
for(int i=;i<m;i++)
{
scan_d(x);scan_d(y);scan_d(val);
if(mapn[x][y]>val)//判重边
{
mapn[x][y]=mapn[y][x]=val;
}
}
for(int i=;i<k;i++)
{
scan_d(q);
for(int j=;j<q;j++)
scan_d(rt[j]);
/*既然已经联通了那么相互之间就不需要再花费钱去修路了*/
for(int j=;j+<q;j++)
mapn[rt[j]][rt[j+]]=mapn[rt[j+]][rt[j]]=;
}
printf("%d\n",prim());
}
return ;
}
Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的的更多相关文章
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 求最小生成树(暴力法,prim,prim的堆优化,kruskal)
求最小生成树(暴力法,prim,prim的堆优化,kruskal) 5 71 2 22 5 21 3 41 4 73 4 12 3 13 5 6 我们采用的是dfs的回溯暴力,所以对于如下图,只能搜索 ...
- 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 ...
随机推荐
- asp.net core封装layui组件示例分享
用什么封装?自然是TagHelper啊,是啥?自己瞅文档去 在学习使用TagHelper的时候,最希望的就是能有个Demo能够让自己作为参考 怎么去封装一个组件? 不同的情况怎么去实现? 有没有更好更 ...
- 考了3年,工作四年,零基础在职终于拿到CFA证书
大家都知道CFA Charterholder是独有的全球公认的投资管理从业人员高职业水平和道德水准的有力证明,是金融界卓越专业成就的象征:CFA资格强调和遵循极其严格的职业操守和道德准则,世界各主要发 ...
- Java中的类型擦除与桥方法
类型擦除 Java在语法中虽然存在泛型的概念,但是在虚拟机中却没有泛型的概念,虚拟机中所有的类型都是普通类.无论何时定义一个泛型类型,编译后类型会被都被自动转换成一个相应的原始类型. 比如这个类 pu ...
- Elixir游戏服设计五
在<Elixir游戏服设计一>里提到,按照系统功能划分成app要保证原子性很难, 现在想想也没那么难.保证原子性,无非就是需要某个单点去完成操作.那么选择玩家进程去做原子性工作就可以了. ...
- 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之注册与登录监听
一.引言 在数据库和静态页面都创建好之后,下面就该接着完成后台Node.js监听注册和登录的部分了.这个部分主要使用的技术是:Node.js的Express框架和ajax异步请求.登录和注册的代码实现 ...
- zoj1797 Least Common Multiple 最小公倍数
Least Common Multiple Time Limit: 2 Seconds Memory Limit: 65536 KB The least common multiple (L ...
- c#中list使用示例
protected void Page_Load(object sender, EventArgs e) { List<string> studentNames = new List< ...
- C语言通过函数参数不能带出动态内存的例子。
实验结论:通过函数参数不能带出动态内存,函数参数虽然为指针,其实是在函数内部的临时变量,只是该指针的初始值是通过调用函数赋值的.C语言函数参数都是传值的. #include <stdio.h&g ...
- Linux向文件添加内容的几种方法
例如,要想test.txt文件添加内容"I am a boy",test.txt在当前目录中 方法一:vi编辑法 打开终端,输入vi test.txt 回车,按a或i进入编辑模式, ...
- 正则表达式与grep和sed
正则表达式与grep和sed 目录 1.正则表达式 2.grep 3.sed grep和sed需要正则表达式,我们需要注意的正则表达式与通配符用法的区分. 1.正则表达式 REGEXP,正则表达式:由 ...