题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598

find the most comfortable road

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3783    Accepted Submission(s):
1617

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 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
 
Sample Output
1
0
 
题目大意:找到速度差值最小的路线即可,这里不要求找到最短路。。。
 
 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int s,d,f;
} p[]; int father[];
const int INF=; void set(int n)
{
for (int i=; i<=n; i++)
father[i]=i;
} bool cmp(const node &a,const node &b)
{
return a.f<b.f;
} int find(int a)
{
if (father[a]==a)return a;
return father[a]=find(father[a]);
} void Union(int x,int y)
{
x=find(x);
y=find(y);
if (x!=y)
father[x]=y;
} int main ()
{
int n,m,Min;
while (~scanf("%d%d",&n,&m))
{
for (int i=; i<=m; i++)
{
scanf("%d%d%d",&p[i].s,&p[i].d,&p[i].f);
}
sort(p+,p+m+,cmp);
int q;
scanf("%d",&q);
while (q--)
{
int a,b,ans,i,j;
Min=INF;
scanf("%d%d",&a,&b);
for (i=; i<=m; i++)
{
set(n);
for (j=i; j<=m; j++)
{
Union(p[j].s,p[j].d);
if (find(a)==find(b))
break;
}
if(j==m+)
// printf ("-1\n");
break;
ans=p[j].f-p[i].f;
if (Min>ans)
{
Min=ans;
}
} if(Min==INF)
printf("-1\n");
else
printf("%d\n",Min);
}
}
return ;
}

hdu 1598 find the most comfortable road (并查集+枚举)的更多相关文章

  1. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  2. hdu 1598 find the most comfortable road (并查集)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  5. HDU 1598 find the most comfortable road (MST)

    find the most comfortable road Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  6. hdu 1598 find the most comfortable road(并查集+枚举)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. HDU - 1598 find the most comfortable road 【最小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...

  8. HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合

    Problem Description XX星有很多城市,城市之间通过一种奇怪的快速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...

  9. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)

    一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...

随机推荐

  1. matplotlib中什么是后端

    在很多网上文档和邮件列表中提到了"后端",并且很多初学者会对这个术语迷惑.matplotlib把不同使用情形和输出格式作为目标.一些人用matplotlib在python shel ...

  2. Java InputStream转File

    文件处于磁盘上或者流处于内存中 在输入流有已知的和预处理的数据时,如在硬盘上的文件或者在流处于内存中.这种情况下,不需要做边界校验,并且内存容量条件允许的话,可以简单的读取并一次写入. InputSt ...

  3. Ajax在jQuery中的应用($.ajax()方法)

    Ajax() 方法 $.ajax() 中的参数及使用方法 在jQuery中,$.ajax() 是最底层的方法,也是功能最强的方法.$.get().$.post().$.getScript().getJ ...

  4. html5 isPointInPath相关操作

    <body> <canvas id="> </canvas> <script type="text/javascript"> ...

  5. BZOJ 2241 打地鼠(特技暴力)

    果然暴力出奇迹.. O(n^2m^2)=1e8 536ms能过. 枚举锤子的长和宽,再验证是否可以满足条件并更新答案. 我们先从左上角为(1,1)的先锤,显然锤的次数是a[1][1]. 锤(i,j)的 ...

  6. 为windows phone listbox 添加触摸倾斜效果

    在开发windows phone程序时,经常会用到listbox或者是longlistselector等列表控件.当点击时没有触摸效果体验会稍差一些,像windows phone中的设置页面一样,点击 ...

  7. 最新发现:Object.defineProperty()让数组的length属性变成只读

    将属性添加到对象, 或修改现有属性的特性---MSDN的解释. 语法 object.defineProperty(object, propertyname, descriptor) object: 必 ...

  8. [洛谷4609] [FJOI2016]建筑师

    题目描述 LOJ题面:https://loj.ac/problem/2173. 洛谷题面:https://www.luogu.org/problemnew/show/P4609. Solution [ ...

  9. [洛谷P4999]烦人的数学作业

    题目大意:定义$f(x)$表示$x$每一个数位(十进制)的数之和,求$\sum\limits_{i=l}^rf(i)$,多组询问. 题解:数位$DP$,可以求出每个数字的出现个数,再乘上每个数字的大小 ...

  10. 【HUD-5790】Prefix (主席树+tire)

    似乎是归队赛的最后一道题. 由于当时以为是公共字串所以没写555555,其实是求公共前缀. 做法是建立tire,把tire上的点编号看成是值,查询第l到第r个字符串的区间内不重复的值的个数.建立主席树 ...