题目链接: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. ZOJ 1403 F-Safecracker

    https://vjudge.net/contest/67836#problem/F "The item is locked in a Klein safe behind a paintin ...

  2. ES6之 =>箭头函数

    原文,请点此链接http://www.cnblogs.com/allenxieyusheng/p/5784728.html 1. 第一个函数 ()=>1 解析:其实这是一个匿名函数直接执行 (f ...

  3. Ninject学习资料

    https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel http://www.cnblogs.com/willick/p/3223 ...

  4. BZOJ3052 & UOJ58:[WC2013]糖果公园——题解

    http://uoj.ac/problem/58 http://www.lydsy.com/JudgeOnline/problem.php?id=3052 输入格式 输出格式 input 4 3 5 ...

  5. CC TSUBSTR:Substrings on a Tree——题解

    https://www.codechef.com/problems/TSUBSTR https://vjudge.net/problem/CodeChef-TSUBSTR 给一棵点权为字母的树,你只能 ...

  6. 2016多校联合训练1 D题GCD (ST表+二分)

    暑假颓废了好久啊...重新开始写博客 题目大意:给定10w个数,10w个询问.每次询问一个区间[l,r],求出gcd(a[l],a[l+1],...,a[r])以及有多少个区间[l',r']满足gcd ...

  7. TYVJ1423 GF和猫咪的玩具

    Description: GF同学和猫咪得到了一个特别的玩具,这个玩具由n个金属环(编号为1---n),和m条绳索组成,每条绳索连接两个不同的金属环,并且长度相同.GF左手拿起金属环L,猫咪右手(或者 ...

  8. Eclipse集成Android NDK及导出Jar和so动态库

    一.安装Cygwin 在Windows环境而又不想使用linux环境,可以安装cygwin(http://www.cygwin.com/ ),为了使用gcc注意cygwin的必选安装包在devel目录 ...

  9. bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作

    Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2173  Solved:  ...

  10. ACM1325Is it A tree?

    通过这道简单而又坑人的题目,练习并查集和set 容器的使用: Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...