HDU1598 find the most comfortable road
来源:互联网 发布:韩火火淘宝店 编辑:程序博客网 时间:2024/06/10 08:23
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 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
Sample Output
1
但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 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
Sample Output
1
0
并查集模板,一开始根本没有往并查集方面想,只想着怎样用prim做,后来听学长说是并查集的模板题,看了同学的代码才了解。
将输入的公路按速度从小到大排列,然后遍历从输入的各个城市出发,将从起点到达终点,所得的最高速与最低速的差中,找出A到B点的最小速度差。
用两层for循环,第一层for 循环指从哪一个城市出发,第二层for循环用来更新根结点。
第二层for循环中,merge()函数用来将两个城市的根结点合并成同一个根结点,用getf()函数找到A点和B点的根结点,
若A点和B点的根结点相同,则更新速度差的最小值。
#include<stdio.h>#define INF 0x3f3f3f3f#include<algorithm>using namespace std;int f[210],n;struct note{ int u,v,w;} que[1010];int cmp(note a,note b){ return a.w<b.w;}void init(){ for(int i=1; i<=n; i++) f[i]=i;}int getf(int v){ if(f[v]==v) return v; f[v]=getf(f[v]); return f[v];}void merge(int a,int b){ int t1=getf(a); int t2=getf(b); if(t1!=t2) f[t2]=t1;}int main(){ int m,q,a,b; while(~scanf("%d%d",&n,&m)) { for(int i=0; i<m; i++) scanf("%d%d%d",&que[i].u,&que[i].v,&que[i].w); sort(que,que+m,cmp); scanf("%d",&q); while(q--) { scanf("%d%d",&a,&b); int minn=INF; for(int i=0; i<m; i++)//从各个城市出发 { init(); for(int j=i; j<m; j++) { merge(que[j].u,que[j].v);//将两个城市连通 if(getf(a)==getf(b)) //如果a城市和b城市连通,更新最小速度差 { if(minn>que[j].w-que[i].w) minn=que[j].w-que[i].w; break; } } } if(minn==INF) printf("-1\n"); else printf("%d\n",minn); } } return 0;}
阅读全文
0 0
- hdu1598 find the most comfortable road
- hdu1598 find the most comfortable road
- hdu1598 find the most comfortable road
- HDU1598-find the most comfortable road
- hdu1598 find the most comfortable road
- HDU1598 find the most comfortable road
- find the most comfortable road||HDU1598
- hdu1598 find the most comfortable road 并查集
- hdu1598 find the most comfortable road 并查集+贪心
- hdu1598,find the most comfortable road,并查集,贪心
- HDU1598 find the most comfortable road 【并查集】+【枚举】
- HDU1598 find the most comfortable road【并查集】
- hdu1598 find the most comfortable road(并查集)
- HDU1598 find the most comfortable road(并查集+kruskal)
- find the most comfortable road
- find the most comfortable road
- find the most comfortable road
- find the most comfortable road
- php中GET方法加号变成空格
- 去除list集合中重复项的几种方法
- POJ 3233 Matrix Power Series
- 陆奇最新演讲:如何成为一个优秀的工程师
- 新手上路之JavaScript 全局变量与局部变量同名问题 详解
- HDU1598 find the most comfortable road
- Linux的which指令
- [Qt] 图的可视化编程
- MFC计算器项目——单位换算模块
- POJ----1426
- Android Studio常见问题以及解决方式
- select
- java设计模式之---策略模式
- C#的协变和逆变