HDU find the most comfortable road
来源:互联网 发布:印度崛起 知乎 编辑:程序博客网 时间:2024/05/19 02:02
Problem Description
XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
但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, 表示寻路的起终点。
第一行有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
每个寻路要求打印一行,仅输出一个非负整数表示最佳路线的舒适度最高速与最低速的差。如果起点和终点不能到达,那么输出-1。
Sample Input
4 41 2 22 3 41 4 13 4 221 31 2
Sample Output
10
题解
这一题的思路有点巧,福建省2014年冬令营也有过类似的题目,以下是我的想法:
我们可先将道路排好序,一个指针w=1。最大值maxl=-1,最小值minl=-1,当w<m时做以下操作:
1.从w开始,向后做生成树,直到询问的起终点在同一集合中,并将在这一过程中最后扫描到的路径编号赋给w,最后扫描到的路径长度为这条可行路的最大值maxl(因为边以从小到大排序)。
2. 如果maxl=-1,则表示起终点无法到达,输出-1.
3.之后,再从w开始往回做生成树,直到询问的起终点在同一集合中,最后扫描到的路径长即为最小值minl,w=最后扫描到的路径编号+1。
4.重复上述环节。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
wikioi的舒适的路线也是同样的作法。
代码如下:
#include<cstdio>#include<iostream>#include<cstring>#include<cstring>#include<cmath>#include<algorithm>#define INF 0x7fffffffusing namespace std;int n,m,q,zz,fa[205],ans;struct bian{int fr,to,v;} e[1002];void insert(int x,int y,int z){zz++; e[zz].fr=x; e[zz].to=y; e[zz].v=z;}bool kp(const bian &a,const bian &b){if(a.v<b.v) return true;else return false;}int find(int x){if(fa[x]!=x) fa[x]=find(fa[x]);return fa[x];}void doit(int s,int t){int maxl,minl,w=1;while(w<zz) {maxl=-1; minl=-1;for(int i=1;i<=n;i++) fa[i]=i;for(int i=w;i<=zz;i++) {int r1=find(e[i].fr), r2=find(e[i].to); fa[r2]=r1; if(find(s)==find(t)) {maxl=e[i].v; w=i; break;} } if(maxl==-1) return ; for(int i=1;i<=n;i++) fa[i]=i; for(int i=w;i>=1;i--) {int r1=find(e[i].fr), r2=find(e[i].to); fa[r2]=r1; if(find(s)==find(t)) {minl=e[i].v; w=i+1; break;} } ans=min(ans,maxl-minl); } }int main(){while(scanf("%d%d",&n,&m)!=EOF) { zz=0;for(int i=1;i<=m;i++) {int x,y,z; scanf("%d%d%d",&x,&y,&z); insert(x,y,z); }sort(e+1,e+zz+1,kp);scanf("%d",&q);for(int i=1;i<=q;i++) {int x,y; ans=INF; scanf("%d%d",&x,&y); doit(x,y); if(ans!=INF) printf("%d\n",ans); else printf("-1\n"); } }return 0;}
0 0
- find the most comfortable road hdu 1598
- HDU-1598-find the most comfortable road
- hdu 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1304 find the most comfortable road
- hdu find the most comfortable road
- HDU 1598 find the most comfortable road
- HDU find the most comfortable road
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- 文件加结构体
- GCC下用C写for循环延迟函数并且不被优化的实现
- Source Insight工程中tab键显示乱码解决方法
- 正则表达式初步
- Maven学习 (二) Eclipse 上安装 Maven3插件
- HDU find the most comfortable road
- 介绍linux下vi命令的使用
- MFC 鼠标消息
- MyBatis的动态SQL详解
- dup and dup2的剖析
- C/C++ACM入门之输入函数格式控制
- Struts2(三)——数据在框架中的数据流转问题
- Objective-c官方文档 怎么自定义类
- 九度1023