HDU 1598 find the most comfortable road 贪心+并查集
来源:互联网 发布:mac口红dangerous 编辑:程序博客网 时间:2024/05/17 22:58
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
解题思路:求一个点u到另一个点v的路径中最大值减最小值的差最小,最容易的方法就是找出所有 u到v的路径让后比较每条路径上最大值和最小值的差值,找出符合题意的,显然这是一个很笨的方法也不太好实现,因为以前做过一个题,是让求一个点到另一个点所经过路径中的最大值,那时用的是prim算法找一个点到另一个点的最小生成树中的最大权值,显然对于这个题也是不太合适的,因为会出现这种情况:
1 2 2
2 3 4
1 4 1
3 4 2
求1 - 2 的本来应该是0,但在prim算法中由于先加入的是权值为 1 的顶点4,然后才加2,所以这时最大值 减最小值的值为1,后来发现可以借助kruskal算法的思想来解决这个题,先把所有的路径从小到大排序,然后用并查集将所有的带权路径合并,直到找到起点和终点在一个集合中,找到然后不断更新最小值最后输出即可
#include <bits/stdc++.h>#define INF 0x3f3f3f3fusing namespace std;struct Node{ int u,v,w;};Node edge[205*205];int n,m,pre[205];bool cmp(Node a,Node b){ return a.w < b.w;}void Init(){ for(int i = 0; i <= n; i++) pre[i] = i;}int Find(int x){ if(x == pre[x]) return x; else return pre[x] = Find(pre[x]);}void mix(int x, int y){ int fx = Find(x),fy = Find(y); if(fx !=fy) pre[fy] = fx;}int main(){ while(~scanf("%d %d",&n,&m)){ for(int i = 0; i < m; i ++) scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w); sort(edge,edge+m,cmp); int Q,s1,e1; cin>>Q; while(Q--){ int ans = INF; scanf("%d %d",&s1,&e1); for(int i = 0; i < m; i ++){ Init(); for(int j = i; j < m; j ++){ mix(edge[j].u,edge[j].v); if(Find(s1) == Find(e1)){ ans = min(ans,edge[j].w-edge[i].w); break; } } } if(ans == INF) cout<<"-1"<<endl; else cout<<ans<<endl; } } return 0;}
阅读全文
0 0
- 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 贪心+并查集
- find the most comfortable road(并查集+贪心)
- hdu1598 find the most comfortable road 并查集+贪心
- hdu1598,find the most comfortable road,并查集,贪心
- 【hdu】 find the most comfortable road (并查集)
- find the most comfortable road(hdu并查集+暴力)
- hdu 1598 find the most comfortable road 枚举+并查集+贪心(类似最小生成树) ★★★
- hdoj 1598 find the most comfortable road(并查集)
- HDOJ 1598 find the most comfortable road 并查集
- find the most comfortable road 1598 (并查集+枚举)
- css学习笔记之line-height
- 计算一个整数二进制位中1的个数。要求效率尽可能的高。且能正确求正数和负数的二进制中1的个数。
- this的指向问题
- CSS选择器
- 对于webSocket的学习及原理了解
- HDU 1598 find the most comfortable road 贪心+并查集
- 排序算法之希尔排序
- 在Vmware中安装CentOS7
- centos7下apache2.4.6虚拟主机配置
- Font Awesome图标字体库
- CNI:容器网络接口详解
- 计算糖果
- 排序算法之选择排序
- ORACLE 存储过程死锁