题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1598

题目大意:

XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。

解题思路:

对边进行排序,从小到大枚举下界,依次加入每条边,知道恰好目标点连通,那么正好是最大值-最小值。答案取最小值

 #include<bits/stdc++.h>
using namespace std;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
struct node
{
int u, v, w;
bool operator <(const node & a)const
{
return w < a.w;
}
}a[maxn];
int p[maxn];
void init()
{
for(int i = ; i < maxn; i++)p[i] = i;
}
int Find(int x)
{
return x == p[x] ? x : p[x] = Find(p[x]);
}
void Union(int x, int y)
{
p[Find(x)] = Find(y);
}
int main()
{
int n, m;
while(cin >> n >> m)
{
for(int i = ; i < m; i++)
{
cin >> a[i].u >> a[i].v >> a[i].w;
}
sort(a, a + m);
int t, u, v;
cin >> t;
while(t--)
{
cin >> u >> v;
int ans = INF;
for(int start = ; start < m; start++)
{
init();
for(int end = start; end < m; end++)
{
Union(a[end].u, a[end].v);
if(Find(u) == Find(v))
{
ans = min(ans, a[end].w - a[start].w);
}
}
}
if(ans < INF)cout<<ans<<endl;
else cout<<"-1"<<endl;
}
}
return ;
}

hdu-1598 find the most comfortable road---kruskal+枚举下界的更多相关文章

  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 (并查集+枚举)

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

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

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

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

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

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

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

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

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

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

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

  8. HDU 1589 Find The Most Comfortable Road 最小生成树+枚举

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

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

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

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

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

随机推荐

  1. js对象转换为json字符串

    JavaScript的对象是一组由键-值组成的无序集合,例如: var person = { name: 'Bob', age: 20, tags: ['js', 'web', 'mobile'], ...

  2. Node.js 内置模块crypto使用事件方法(onreadable)加密的一些问题

    javaScript代码如下: 'use strict'; const crypto = require('crypto'); //实例化一个AES加密对象 const aesEncrept = cr ...

  3. Mysql的子查询与连接查询

    子查询: 在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句 主查询和子查询的关系: 子查询是嵌入到主查询中,子查询是辅助主查询的,要 ...

  4. POJ2388-Who's in the Middle

    题目链接:点击打开链接 Who's in the Middle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45683   ...

  5. JMETER从JSON响应中提取数据

    如果你在这里,可能是因为你需要使用JMeter从Json响应中提取变量. 好消息!您正在掌握掌握JMeter Json Extractor的权威指南.作为Rest API测试指南的补充,您将学习掌握J ...

  6. 1088 Rational Arithmetic(20 分)

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...

  7. C# Task任务详解及其使用方式

    https://blog.csdn.net/younghaiqing/article/details/81455410 C#多线程编程笔记(4.3)-Task任务中实现取消选项 https://blo ...

  8. 使用 dbutils 的结果集包装类 StringTrimmedResultSet

    1.功能 StringTrimmedResultSet 的功能是去掉结果集中数据的前后空格,这个方法是在取结果的时候处理. 2.使用 一般在新建 QueryRunner 对象的时候使用: QueryR ...

  9. TortoiseSVN 搭建本地版本库及简单操作使用

    TortoiseSVN是windows上一款著名的版本控制软件,对于我们管理自己的代码,特别是对一个团队来说,非常重要. 本文探讨的是如何搭建本地的版本库. (1)安装TortoiseSVN之后需要创 ...

  10. (转)Bash Shell常用快捷键

    Bash Shell常用快捷键 原文:https://github.com/hokein/Wiki/wiki/Bash-Shell%E5%B8%B8%E7%94%A8%E5%BF%AB%E6%8D%B ...