hdu1598贪心+并查集的巧妙用法
来源:互联网 发布:好用的会计软件 编辑:程序博客网 时间:2024/05/22 08:03
find the most comfortable road
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7842 Accepted Submission(s): 3298
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
Author
#include<cstdio>#include<iostream>#include<string.h>#include<algorithm>using namespace std;int f[510];int n,m,q;struct edge{int u;int v;int w;}e[1010];bool cmp(edge a,edge b){return a.w<b.w; } void init(){for(int i=1;i<=n;i++)f[i]=i;}int find(int x){return x==f[x]?x:f[x]=find(f[x]);}void Merge(int x,int y){int xx=find(x);int yy=find(y);if(xx!=yy)f[xx]=yy;}int main(){while(scanf("%d%d",&n,&m)!=EOF){int ans;for(int i=0;i<m;i++)scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);sort(e,e+m,cmp);scanf("%d",&q);while(q--){int x,y;int i,j;ans=INT_MAX;scanf("%d %d",&x,&y);for(i=0;i<m;i++){init();int f=0,k=0;for(j=i;j<m;j++){Merge(e[j].u,e[j].v);if(find(x)==find(y)){k=j;f=1;break;}}if(!f) break;ans=min(ans,e[k].w-e[i].w);}if(ans==INT_MAX)printf("-1\n");else printf("%d\n",ans);}}}
ailyanlu
Source
HDU 2007-Spring Programming Contest - Warm Up (1)
思路:
先把,每条边从小到大排,分别以每条边为起点并查集查一下时候是所查询的点的集合,以此更新答案。
阅读全文
0 0
- hdu1598贪心+并查集的巧妙用法
- hdu1598 并查集+贪心
- 并查集的应用hdu1598
- hdu1598 find the most comfortable road 并查集+贪心
- hdu1598,find the most comfortable road,并查集,贪心
- hdu1598之并查集
- hdu1598(枚举法+并查集)
- HDU1598 并查集 或 二分+DFS
- hdu1598 有向边 并查集
- HDU1598 find the most comfortable road (最小生成树,并查集的应用)
- 最舒适的路(并查集+枚举)(hdu1598)
- 并查集巧妙用法(codeforces)C. Destroying Array
- 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)
- 贪心+并查集
- Unity 算法功能 日記
- 最长回文字串(JAVA实现)
- 找不到或无法加载主类
- NYOJ 中缀式变成后缀式(栈)
- python取数作为临时极大值(极小值)
- hdu1598贪心+并查集的巧妙用法
- centos7 下因/etc/rc.d/rc.local文件出错导致系统起不来
- 剑指offer——二叉树的镜像
- Linux命令详解
- 《好未来编程题》n个数里最小的k个
- dp基础4
- 2017.08.10
- 一个tomcat配置不同端口访问同一个项目
- camera插值(高通LL_8916平台)