hdu-1598 find the most comfortable road---kruskal+枚举下界
题目链接:
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+枚举下界的更多相关文章
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 1598 find the most comfortable road (并查集)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1598 find the most comfortable road (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 1598 find the most comfortable road(并查集+枚举)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1589 Find The Most Comfortable Road 最小生成树+枚举
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合
Problem Description XX星有很多城市,城市之间通过一种奇怪的快速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...
- HDU - 1598 find the most comfortable road 【最小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...
随机推荐
- window 环境下在虚拟机上安装php环境
转发:https://www.cnblogs.com/orangegem/p/7191659.html 安装linux工具 :https://blog.csdn.net/z15732621582/ar ...
- Codeforces Round #520 (Div. 2)B(贪心,数学)
#include<bits/stdc++.h>using namespace std;int mi[100007];int main(){ int cnt=0; int flag=0; i ...
- 2017-10-20 NOIP模拟赛
Lucky Transformation #include<iostream> #include<cstring> #include<cstdio> using n ...
- Windows日志为什么要把它转成Syslog呢?
有的朋友会问,好好的Windows日志为什么要把它转成Syslog呢?呵呵,当Windows服务器比较少的时候,我们是不需要这样做的.但试想如果你管理着成千上百台的Windows机器,你会一台一台的登 ...
- Hadoop集群配置免密SSH登录方法
Hadoop集群包含1个主节点和3个从节点,需要实现各节点之间的免密码登录,下面介绍具体的实现方法. 一.Hadoop集群环境 二.免密登录原理 每台主机authorized_keys文件里面包含的主 ...
- 帝都Day4(3)——还是数据结构
可并堆 左偏树中 dist[x]=dist[rs[x]]+1 合并的时候,把权志较大的根作为根节点,把这棵树右子树和另一棵树合并. 说明白点:(上图描述有点问题) 设x表示根权值较大的左偏树,y表示根 ...
- DHCPv6协议
DHCPv6协议 1. 定义 IPv6 动态主机配置协议DHCPv6(Dynamic Host Configuration Protocol for IPv6)是针对IPv6编址方案设计,为主 ...
- javascript中的Set和Map数据结构
Set数据结构 类似数组,所有的数据都是唯一的,没有重复值,它本身是一个构造函数 size 数据长度 add() 添加一个数据 delete() 删除一个数据 has() 查找一个数据 clear() ...
- VUE中嵌套路由
官网地址:https://router.vuejs.org/zh-cn/essentials/nested-routes.html 路由嵌套一般使用在后台管理系统中 给一个比较简单的小案例 <! ...
- Java中常用的数据源
几种常用的Java数据源解决方案 Java中的数据源就是javax.sql.DataSource.DataSource的创建可以有不同的实现. JNDI方式创建DataSource 以JNDI方式创建 ...