find the most comfortable road||HDU1598
来源:互联网 发布:网络视听审核员证书 编辑:程序博客网 时间:2024/05/19 06:36
link:http://acm.hdu.edu.cn/showproblem.php?pid=1598
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
题解:并查集加贪心,先给所有路的速度从小到大排序,然后枚举,挨个连接已有的路,找到给出的两点之间的通路,因为之前排过序,所以这一层循环里找到的第一条通路,就是已知的最舒服的路
AC代码:
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<iostream>using namespace std;int INF=0x3f3f3f3f;int par[300];int n;struct edge{ int start,end,speed;}q[1010];void initial(){ for(int i=1;i<=n;i++) par[i]=i;}int find(int x){ if(x!=par[x]) par[x]=find(par[x]); return par[x];}void unite(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) par[fx]=fy;}bool cmp(edge a,edge b){ return a.speed<b.speed;}int main(){ int m,i,j,mmin,x,s,t; while(~scanf("%d%d",&n,&m)) { for(i=0;i<m;i++) scanf("%d%d%d",&q[i].start,&q[i].end,&q[i].speed); scanf("%d",&x); sort(q,q+m,cmp); while(x--) { scanf("%d%d",&s,&t); mmin=INF; for(i=0;i<m;i++) { initial(); for(j=i;j<m;j++) { unite(q[j].start,q[j].end); if(find(s)==find(t)) { mmin=min(mmin,q[j].speed-q[i].speed); break; } } } if(mmin==INF) { printf("-1\n"); } else printf("%d\n",mmin); } }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
- SQL LIKE操作符
- three.js全景视频
- HDU4237 The Rascal Triangle
- DNS
- C++知识总结(4)
- find the most comfortable road||HDU1598
- Just a Hook (HDU
- 数据源--Springboot配置使用Druid数据源
- 1069. The Black Hole of Numbers (20) -----C++
- 解决pycharm中中文列表输出'\xe5\xa4\xa7\xe8\x92\x9c'之类的字符串
- 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/ch
- Android动画效果之- Property Animation(属性动画)
- hdu 4027 Can you answer these queries? 线段树
- 记录一下有关PCA的PWM