HDU3371--Connect the Cities(最小生成树)
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
Author
dandelion
Source
HDOJ Monthly Contest – 2010.04.04
Recommend
lcy
又是最小生成树的裸题
这是代码
#include<stdio.h>
#include<string.h>
#define N 505
#define INF 0x7ffffff
int g[N][N];
int low[N],vis[N];
int n;
int prim()
{
int pos,res=0;
memset(vis,0,sizeof(vis));
vis[1]=1;
pos=1;
for(int i=1;i<=n;i++)
if(i!=pos) low[i]=g[pos][i];
for(int i=1;i<n;i++){
int minn=INF;
pos=-1;
for(int j=1;j<=n;j++)
if(!vis[j] && minn>low[j]){
minn=low[j];
pos=j;
}
if(pos==-1) return -1; //注意这里
res+=minn;
vis[pos]=1;
//printf("**%d %d\n",pos,minn);
for(int j=1;j<=n;j++)
if(!vis[j] && low[j]>g[pos][j])
low[j]=g[pos][j];
}
return res;
}
int main(void)
{
int t,m,k;
int u,v,d;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
g[i][j]=INF;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&d);
if(g[u][v]>d) //notice
g[u][v]=g[v][u]=d;
}
for(int i=0;i<k;i++){
scanf("%d",&d);
scanf("%d",&u);
d--;
while(d--){
scanf("%d",&v);
g[u][v]=g[v][u]=0;
u=v;
}
}
printf("%d\n",prim());
}
return 0;
}
HDU3371--Connect the Cities(最小生成树)的更多相关文章
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu3371 Connect the Cities (MST)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- POJ:3371 Connect the Cities(最小生成树)
http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- HDU3371 Connect the Cities
题目描述: 有n个小岛,其中有的小岛之间没有通路,要修这样一条通路需要花费一定的钱,还有一些小岛之间是有通路的.现在想把所有的岛都连通起来,求最少的花费是多少. 输入: 第一行输入T,代表多少组数据. ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 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/Other ...
随机推荐
- PowerTool(杀毒辅助工具) V4.6 中文免费绿色版
软件名称: PowerTool(杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 968KB图片预览: 软件简 ...
- 《HTML5与CSS3权威指南》读书笔记(上册)—HTML5篇
豆瓣上的评分少且评价不太好,阅读当中发现几处刊物上的小问题,不过线下口碑貌似不错,基本上人手一本 上册五百多页,主讲H5,分为标签,本地存储,离线应用程序,新的API,获取地理位置信息标签包含表单,C ...
- Jquery实现双击表单元格可编辑
<script type="text/javascript"> function doTableTdEditClick(param){ doTdEditable(par ...
- sql convert() 函数
convert: 时间格式转换为其他时间格式的函数 CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) data_type: ...
- instanceof运算符、Class的isInstance( )与isAssignableFrom之间的区别
instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false.形象地:自身实例或子类实例 instance ...
- FR javascript 时间设置上个月最后一天后当月最后一天
//设置上月最后一天 var date1 =new Date(); date1.setDate(1); //first day; date1.setMonth(date1.getMonth()); / ...
- openstack私有云布署实践【7.2 keystone + memcache (办公网环境)】
首先登录controller1创建keystone数据库,并赋于远程和本地访问的权限. mysql -u root -p CREATE DATABASE keystone; GRANT ALL P ...
- Android 关于 OnScrollListener 事件顺序次数的简要分析
在 Android 的 OnScrollListener 整个事件我主要分析下他的执行顺序: 实现滚动事件的监听接口 new AbsListView.OnScrollListener(){ @Over ...
- express学习点滴- session()和cookieSession()的区别
express 里提供了两种有关session的中间件 * session() 提供了内存和数据库两种方式保存session.具体两种session原理请自行学习,不进行展开了.自己也是一知半解... ...
- 第一百零一节,JavaScript流程控制语句
JavaScript流程控制语句 学习要点: 1.语句的定义 2.if 语句 3.switch语句 4.do...while语句 5.while语句 6.for语句 7.for...in语句 8.br ...