题目链接: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. PAT 甲级 1032 Sharing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...

  2. PHPcms企业黄页中,会员注册之后提示经营模式不得少于1个字符 的解决办法

    后台--模块- 黄页模块 ---  企业库 --- 字段 ---   经营模式 ---  字符长度取值范围 1  改为  0. 

  3. bzoj4555-求和

    题目 \(S(i,j)\)表示第二类斯特林数,求: \[ f(n)=\sum _{i=0}^n\sum _{j=0}^iS(i,j)*2^j*j! \] 分析 公式推理很简单,关键是用到了第二类斯特林 ...

  4. 【zoj2314】Reactor Cooling 有上下界可行流

    题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...

  5. BZOJ3167/BZOJ4824 HEOI2013SAO/CQOI2017老C的键盘(树形dp)

    前者是后者各方面的强化版. 容易想到设f[i][j]表示i子树中第j小的是i的方案数(即只考虑相对关系).比较麻烦的在于转移.考虑逐个合并子树.容易想到枚举根原来的排名和子树根原来的排名,算一发组合数 ...

  6. Android SDK Manager下载,解决方案

    一.Windows 平台 在C:\Windows\System32\drivers\etc\hosts文件.添加一行:74.125.237.1       dl-ssl.google.com 二.Li ...

  7. 【题解】SCOI2007组队

    恩……为什么大家都这么执着于 \(O(n^{2})\) 的复杂度捏?如果接受 \(O(n^{2} + nV)\) 的复杂度,那这题可不是道**题吗( • ̀ω•́ )✧ 首先把所有的人按照身高排个序, ...

  8. 【题解】HNOI2016序列

    也想了有半天,没有做出来……实际上做法确实也是十分精妙的.这里推荐一个blog,个人认为这位博主讲得挺好了:Sengxian's Blog; 感觉启示是:首先要加强对莫队算法 & ST表的熟练 ...

  9. [洛谷P4781]【模板】拉格朗日插值

    题目大意:给你$n(n\leqslant2000)$个点,要你求$n-1$次经过这$n$个点的多项式在$k$处的值 题解:$Lagrange$插值:$$f_x=\sum\limits_{i=1}^ky ...

  10. [洛谷P3833][SHOI2012]魔法树

    题目大意:给一棵树,路径加,子树求和 题解:树剖 卡点:无 C++ Code: #include <cstdio> #include <iostream> #define ma ...