图论之并查集-find the most comfortable road
来源:互联网 发布:网络销售金融产品 编辑:程序博客网 时间:2024/05/02 04:31
题目如下:
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
#include<map>#include<set>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<vector>#include<string>#include<stdio.h>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int n,m;int f[210];int rank1[210];struct node{int u;int v;int speed;}a1[210*210];int cmp(node x,node y){return x.speed<y.speed;}void init(){int i;for(i=0;i<210;i++){f[i]=i;rank1[i]=0;}}int find(int x){int i,j=x;while(f[j]!=j)j=f[j];while(x!=j){i=f[x];f[x]=j;x=i;}return j;}void Union(int x,int y){int a=find(x),b=find(y);if(a==b)return;if(rank1[a]>rank1[b])f[b]=a;else{f[a]=b;//必须从根节点处合并,不然前面的那些节点就会被省略掉,无法合并进去if(rank1[a]==rank1[b])rank1[b]++;}}int min(int x,int y){if(x<y)return x;elsereturn y;}int main(){int i,k;while(~scanf("%d%d",&n,&m))//注意题目中指明输入包含多个实例{for(i=0;i<m;i++){scanf("%d%d%d",&a1[i].u,&a1[i].v,&a1[i].speed);}sort(a1,a1+m,cmp);int num;scanf("%d",&num);while(num--){int st,en;scanf("%d%d",&st,&en);int c=0x3f3f3f3f;for(i=0;i<m;i++){init();for(k=i;k<m;k++){Union(a1[k].u,a1[k].v);if(find(st)==find(en)){c=min(c,a1[k].speed-a1[i].speed);break;}}}if(c==0x3f3f3f3f)c=-1;printf("%d\n",c);}}return 0;}
附:
搜索方法可以改为用rear和end的方法,即先把他们都设置为1,然后end从一开始搜索,一直到能包含我们要查找的那两个点,记录ans,之后再使rear从1开始增加,一直到不包含我们所要查找的点或他们两个相等,在此过程中要不断更新ans,然后再把end往后加这样一直循环到rear和and都到达m,这样只需要搜索2*m次即可得到结果。
1 0
- 图论之并查集-find the most comfortable road
- find the most comfortable road(并查集与图论)
- hdu1598 find the most comfortable road 并查集
- hdoj 1598 find the most comfortable road(并查集)
- 【并查集】find the most comfortable road
- 【hdu】 find the most comfortable road (并查集)
- find the most comfortable road(并查集)
- find the most comfortable road(并查集+贪心)
- hdu1598 find the most comfortable road 并查集+贪心
- find the most comfortable road + 并查集+枚举
- find the most comfortable road(hdu并查集+暴力)
- hdu 1598 find the most comfortable road(并查集)
- hdu1598,find the most comfortable road,并查集,贪心
- HDU1598 find the most comfortable road 【并查集】+【枚举】
- HDOJ 1598 find the most comfortable road 并查集
- hdu 1598 find the most comfortable road 并查集
- find the most comfortable road 1598 (并查集+枚举)
- HDU1598 find the most comfortable road【并查集】
- 【ITOO虐心之旅】——纵你虐我千百遍,我仍视你为初恋
- 我给小楠讲历史—上下五千年
- 数据结构实验之二叉树二:遍历二叉树
- oracle sys/system用户的默认密码
- hd 2612 Find a way (BFS)
- 图论之并查集-find the most comfortable road
- tjut 4665
- 不敢死队问题
- 实现一个函数翻转字符串(将一个字符串的内容,逆序存储)。
- 【CDOJ 1334】 郭大侠与Rabi-Ribi
- 数据结构实验之栈五:下一较大值(一)
- NYOJ 436 sum of all integer numbers(坑人,递归)
- CSS-Border(边框)
- 这是我的第一份博客 flying saucer技术生成pdf文档