HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合
Problem Description
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。
(SARS是双向的)。
Input
第一行有2个正整数n (1<n<=200)和m (m<=1000),表示有N个城市和M条SARS。
接下来的行是三个正整数StartCity,EndCity,speed,表示从表面上看StartCity到EndCity,限速为speedSARS。speed<=1000000
然后是一个正整数Q(Q<11),表示寻路的个数。
接下来Q行每行有2个正整数Start,End, 表示寻路的起终点。
Output
Sample Input
- 4 4
- 1 2 2
- 2 3 4
- 1 4 1
- 3 4 2
- 2
- 1 3
- 1 2
Sample Output
- 1
- 0
- # include<iostream>
- # include<cstdio>
- # include<algorithm>
- using namespace std;
- const int maxn=1000+5;
- struct node{
- int st;
- int ed;
- int fu;
- }road[maxn];
- int p[maxn];
- int st,ed;
- int miin;
- int inf=1000000+5;
- bool cmp(node x,node y)
- {
- return x.fu<y.fu;
- }
- int find(int x)
- {
- return p[x]== x ?
- x:p[x]=find(p[x]);
- }
- int main()
- {
- int n,m;
- while(cin>>n>>m)
- {
- for(int i=1;i<=m;i++)
- scanf("%d%d%d",&road[i].st,&road[i].ed,&road[i].fu);
- sort(road+1,road+m+1,cmp);
- int tot;
- cin>>tot;
- while(tot--)
- {
- miin=inf;
- scanf("%d%d",&st,&ed);
- for(int i=1;i<=m;i++)
- {
- for(int i=1;i<=n;i++) p[i]=i;
- for(int j=i;j<=m;j++)
- {
- int x=find(road[j].st);
- int y=find(road[j].ed);
- if(x!=y) p[x]=y;
- if(find(st)==find(ed))
- {
- int sum=road[j].fu-road[i].fu;
- if(sum<miin) miin=sum;
- break;
- }
- }
- }
- if(miin==inf)
- printf("-1\n");
- else
- printf("%d\n",miin);
- }
- }
- return 0;
- }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合的更多相关文章
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 1598 find the most comfortable road (并查集)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- HDU 1598 find the most comfortable road (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 1598 find the most comfortable road(并查集+枚举)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU - 1598 find the most comfortable road 【最小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- eclipse插件安装验证及问题处理
eclipse插件安装验异常时可看当前workspace下面的.metadata/.log文件,找到具体的问题来处理.一般常用到插件安装不成功的原因如下: 1.jar包冲突: 2.jar包依赖的jav ...
- Eclipse查看某个方法被哪些类调用
方法一:打开该类,在类的定义上即类名上,右键-->References--->Project ,就可以查看该类是否被工程中的其他Java文件引用过:但是如果在JSP页面,这个方法查不出来 ...
- Engine工具栏按钮的使用详解
转自原文 Engine自定义控件实现toolbar功能 Engine提供的工具条能够轻易实现各种操作,非常方便,可是不好的地方就是太死板了,toolbar的图标都不能改.因此需要自己做按钮做控件去实现 ...
- POJ 2185 Milking Grid KMP循环节周期
题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...
- 命令行运行python模块时提示包找不到的问题
庄稼人不是专职python开发的道友.尽管与python相识已多年,可惜相识不相知,仅仅是偶尔借助pydev写一些简单的小工具. 多年来.一直困惑于这样一个问题:相同的project.相同的代码,使用 ...
- C#+HtmlAgilityPack
C#+HtmlAgilityPack—糗事百科桌面版V2.0 最近在浏览以前自己上传的源码,发现在糗事百科桌面端源码评论区中,有人说现在程序不能用了.查看了一下源码运行情况,发现是正则表达式解析问 ...
- 复制相关参数学习笔记--slave上的参数
server_id server_uuid relay_log io_thread 读取过来的本地日志. relaylog文件名前缀,可以是全路径. relay_log_index relay ...
- 【转】优先队列priority_queue 用法详解
http://www.cnblogs.com/void/archive/2012/02/01/2335224.html 优先队列是队列的一种,不过它可以按照自定义的一种方式(数据的优先级)来对队列中的 ...
- RESET MASTER 和RESET SLAVE 命令的使用方法 注意事项
RESET MASTER 删除所有index file 中记录的所有binlog 文件,将日志索引文件清空,创建一个新的日志文件,这个命令通常仅仅用于第一次用于搭建主从关系的时的主库,注意 rese ...
- [React Router v4] Conditionally Render a Route with the Switch Component
We often want to render a Route conditionally within our application. In React Router v4, the Route ...