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 ...
随机推荐
- oracle11g 体系结构详解
1.oracle内存由SGA+PGA所构成 2.oracle数据库体系结构数据库的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制. oracle工作原理: 1).在数据库 ...
- MySQL所学所思所想
MySQL更改线上配置方案思想:原则上,需要备机.备份工作准备到位,有参数调优配置方案.有配置回退方案.有应急切换备机方案.以上方案评审无问题,然后可以和客户约定实施的时间.服务中断时间,先向客户侧申 ...
- macbook 263企业邮箱设置
第一步:打开邮箱,点击添加账号,选择其他 第二步:填写完整的电子邮件地址和密码 第三步:填写收件服务器(popcom.263xmail.com),发件服务器(smtpcom.263xmail.com)
- java集合系列——List集合之ArrayList介绍(二)
一:List概述 List是 java.util包下面的类,从<a href="http://blog.csdn.net/u010648555/article/details/5604 ...
- 针对Openlayer3官网例子的简介
网址:http://openlayers.org/en/latest/examples/ 如果大家想了解ol3能做什么,或者说已提供的API有什么,又闲一个个翻例子跟API累的话,就看看这个吧. 1. ...
- 计算机基础--Java中int char byte的关系
计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...
- C++ sizeof 误区 大公司面试题
1.C++ 无成员变量和函数的类型的实例,求该实例的sizeof? 答:是1.(不是0) 2.如果在题1的基础上有1个成员变量,sizeof是(1+成员变量的大小)吗? 答:不是,是成员变量的大小. ...
- servlet自动获取前端页面提交数据
servlet自动获取前端页面jsp提交数据 以下是本人在学习过程中,因前端页面提交参数过多,后台servlet封装实体类过于麻烦而写的一个工具类,应用于jsp/servlet数据提交后,基于MVC+ ...
- 干了这杯Java之ArrayList
List存储一个有序元素合集 List接口的实现类有: ArrayList,LinkedList,Vector,Stack ArrayList一个数组型的List 默认容量为10 private st ...
- iOS开发必不可少的76个工具
如果你去到一位熟练的木匠的工作室,你总是能发现他/她有一堆工具来完成不同的任务. 软件开发同样如此.你可以从软件开发者如何使用工具中看出他水准如何.有经验的开发者精于使用工具.对你目前所使用的工具不断 ...