HDU1598find the most comfortable road
来源:互联网 发布:郑州青峰网络 编辑:程序博客网 时间:2024/06/04 23:21
虽然是中文题,还是是想说几句题意,是求“并查集”,并不是 最小生成树!!!因为题中说了一句“但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径”,这句话就说明了,你可以随意走,但要求是最舒适,也就是速度差最小!!!比赛的时候没做出来就是理解错题意了,一直用最小生成树一直错!!!
解题思路:主要是并查集方法,但用到了Kruskal的思想,将边从小到大排序。然后每输入一组要求解的数,就开始使用并查集,直到起点和终点连通就更新最小值,如果最小值没有更新,说明无法到达终点。(注意数组大小,因为数组开小了W了好多次( ′⌒`))
XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
第一行有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, 表示寻路的起终点。
4 41 2 22 3 41 4 13 4 221 31 2
10
#include<string.h>#include<stdio.h> #include<algorithm>using namespace std;#define inf 0x3f3f3f3fint n,m,q,f[210];struct edge{ int x,y,len;}e[1010];bool cmp(edge a,edge b){ return a.len<b.len;}void init()//初始化并查集{ for(int i=1;i<=n;i++) f[i]=i;}int getf(int v) //并查集寻找共同的祖先{ if(f[v]==v) return v; else //路径压缩 { f[v]=getf(f[v]); return f[v]; }}int main(){ int i,j,a,b; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].len); sort(e+1,e+m+1,cmp);//将路径从小到大排序 scanf("%d",&q); while(q--) { scanf("%d%d",&a,&b); int minn=inf; for(i=1;i<=m;i++) { init(); //初始化并查集 for(j=i;j<=m;j++) { //合并子集 int t1=getf(e[j].x); int t2=getf(e[j].y); if(t1!=t2) f[t2]=t1; if(getf(a)==getf(b)) //如果起点和终点已经连通,更新最小值,跳出循环 { if(minn>e[j].len-e[i].len) minn=e[j].len-e[i].len; break; } } } if(minn==inf)printf("-1\n"); //minn=inf说明没有更新最小值,也就是没有连通 else printf("%d\n",minn); } } return 0;}
阅读全文
0 0
- hdu1598find the most comfortable road
- hdu1598find the most comfortable road
- HDU1598find the most comfortable road
- HDU1598find the most comfortable road(并查集)
- find the most comfortable road
- find the most comfortable road
- find the most comfortable road
- find the most comfortable road
- find the most comfortable road
- find the most comfortable road
- hdu1598find the most comfortable road(并查集+枚举,求起点到终点的边中最大边减最小边差值最小)
- find the most comfortable road hdu 1598
- HDU-1598-find the most comfortable road
- hdu1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1304 find the most comfortable road
- M - find the most comfortable road
- centos7 libbc.so
- 1265 四点共面
- 以纯粹还原童心,澳洲NUBO儿童室内游乐园设计
- vue加载时闪现模板语法-处理方法
- iOS NSMutableData和NSData常用操作
- HDU1598find the most comfortable road
- mongodb关键指标查看及常规统计命令
- session共享
- 1010. 一元多项式求导 (25)
- Python-面向对象编程
- Android 高级混淆和代码保护技术
- Makefile经典教程(掌握这些足够)
- sqlserver/oracle当插入表数据时同时修改其中一个字段的触发器
- Android中的小技巧