HDU-1598 find the most comfortable road (并查集+枚举+贪心)
来源:互联网 发布:歌曲变调软件下载 编辑:程序博客网 时间:2024/06/07 12:37
find the most comfortable road
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Problem Description
XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),但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
每个寻路要求打印一行,仅输出一个非负整数表示最佳路线的舒适度最高速与最低速的差。如果起点和终点不能到达,那么输出-1。
Sample Input
4 41 2 22 3 41 4 13 4 221 31 2
Sample Output
10
这道题并查集而非最短路如
1—4最短路得差是2,但是正确最小速度差是0;
当最小值minn不变仍为INF没有被更新时输出-1,否则输出minn更新后的的值;
代码:
#include<stdio.h>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;struct edge{ int u; int v; int w;}e[1010];int f[220],m,n;bool cmp(edge a,edge b){ return a.w<b.w;}void init(){ int i; for(i=1;i<=n;i++) f[i]=i;}int findx(int v)//v的父节点;{ if(f[v]==v) { return v; } else { f[v]=findx(f[v]); return f[v]; }}void merge1(int v,int u)//合并{ int t1,t2; t1=findx(v); t2=findx(u); if(t1!=t2) { f[t2]=t1; }}int main(){ while(~scanf("%d%d",&n,&m)) { int i,j; for(i=1;i<=m;i++) { scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); } sort(e+1,e+1+m,cmp);//速度从小到大排; int q,x,y,minn; scanf("%d",&q); while(q--) { scanf("%d%d",&x,&y); minn=INF; for(i=1;i<=m;i++)//枚举 { init(); for(j=i;j<=m;j++) { merge1( e[j].u , e[j].v ); if(findx(x)==findx(y))//x与y联通时,取最小差值; { minn=min( minn , e[j].w-e[i].w); break; } } } if(minn==INF)//如果minn不变输出-1; printf("-1\n"); else printf("%d\n",minn); } } return 0;}
阅读全文
0 0
- HDU-1598 find the most comfortable road (并查集+枚举+贪心)
- find the most comfortable road 1598 (并查集+枚举)
- hdu 1598 find the most comfortable road 枚举+并查集+贪心(类似最小生成树) ★★★
- HDOJ 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 并查集&枚举
- HDU 1598 find the most comfortable road 【枚举+(并查集)最小生成树Kruskal】
- HDU 1598 find the most comfortable road (枚举+Kruskal) 并查集
- HDU 1598 find the most comfortable road(并查集+枚举)
- HDU 1598--find the most comfortable road【并查集 + 枚举】
- hdu 1598 find the most comfortable road(并查集+枚举)
- 4.GC细节
- build Variants创建不同配置的app(图文详解)
- JAVA数组找最大和最小的方法
- 打开一个进程的方法
- Linux学习总结 unit02文件管理
- HDU-1598 find the most comfortable road (并查集+枚举+贪心)
- Windows Server2012R2 FTP服务器配置
- 使用memwatch检查内存泄漏
- PHP连接MySQL数据库
- HDU 2952 Counting Sheep(DFS)
- Imwrite函数如何无损保存图片。
- redis面试
- 将int数组与字符串中的数字互转的方法
- 关于HttpClient HttpGet和HttpPost 请求的运用
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
野画集原版生肉
生肉什么意思
一般生肉包蒸多久才熟
野画集原版生肉21话
如何炒生肉又嫩又烂
啃生肉是什么意思
生肉包馅详细配料配方比例
生肉是什么意思
咖啡遇上香草生肉38生肉
咖啡遇上香草生肉39生肉
白龙为什么不用猫做生肉
没有冰箱怎么保存生肉
虐美人第二季35生肉
8一12岁女童生长发肓表
病入膏肓
病入膏肓意思
病入膏肓什么意思
病入膏肓读音
肓
膏肓穴
学生安全教肓平台登录
肓怎么读
膏肓
膏肓读音
膏肓俞
病在膏肓
南阳市安全教肓平台
唐山市安全教肓平台登录入口
尿道下裂会影响生肓吗
生肖守护神
生肖鸡
2019生肖
一图看生肖
2019年生肖
明年生肖
生肖月份
生肖排行
91年生肖
生肖兔年份
吉祥的生肖
鸡生肖配对