hdu1598
思路:对所有路径的速度从小到大排个序,然后枚举高度差就ok......
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 1<<30
struct node
{
int v1,v2;
int dis;
}s[2000];
int father[2000];
int find(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
int cmp(const node a,const node b)
{
if(a.dis<b.dis)
return 1;
else return 0;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)>0)
{
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&s[i].v1,&s[i].v2,&s[i].dis);
}
sort(s,s+m,cmp);
int q;
scanf("%d",&q);
while(q--)
{
int tmp,tmp1;
int minx=M;
scanf("%d%d",&tmp,&tmp1);
for(int i=0;i<m;i++)
{
for(int k=0;k<=n;k++)
father[k]=k;
for(int j=i;j<m;j++)
{
int x=find(s[j].v1);
int y=find(s[j].v2);
if(x!=y)
father[x]=y;
if(find(tmp)==find(tmp1))
{
if(minx>(s[j].dis-s[i].dis))
{
minx=s[j].dis-s[i].dis;
}
break;
}
}
}
if(minx>=M)
printf("-1\n");
else
printf("%d\n",minx);
}
}
return 0;
}
hdu1598的更多相关文章
- 最舒适的路(并查集+枚举)(hdu1598)
hdu1598 find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768 ...
- *HDU1598 并查集
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
思路: 首先如果想到了Kruskal算法,那么下一步我们可能马上会想:那我们就从头开始写这个算法吧.然后一写就很容易发现一个问题——如果按照正常的Kruskal算法来做,那么start到end的舒适度 ...
- 【类克鲁斯卡尔做法+枚举最小边】【HDU1598】【find the most comfortable road】
题意: 给你一个图,有边权,K个询问:u到v 的路径中 边权最大值-边权最小值的最小值是多少 http://acm.hdu.edu.cn/showproblem.php?pid=1598 题解( ...
- HDU1598 find the most comfortable road 【并查集】+【枚举】
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu1598 find the most comfortable road (枚举)+【并查集】
<题目链接> 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在 ...
- [HDU1598]find the most comfortable road
思路: 考虑一个暴力:枚举最大的边权和最小的边权,然后将边权在这之间的边全拿出来构成一张无向图,剩下的就是判断是否存在一条从$S$到$T$的路径.相当于判$S$和$T$是否连通,用并查集连一下即可.时 ...
- find the most comfortable road(hdu1598)不错的并查集
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
随机推荐
- Ubuntu下架设FTP服务器(转)
Ubuntu下架设FTP服务器 Linux下提供了很多的ftp服务器,这里我选用了安全,快速,简单的vsftpd作为FTP服务器.本文是我在自己的Ubuntu 10.10 -32 位系统下搭建的.搭建 ...
- numpy、scipy、matplotlib、OpenCV安装及问题解决
1 numpy 概述 numpy是Numerical Python的缩写,释义为数值的Python numpy弥补了作为通用编程语言的Python在数值计算方面能力弱.速度慢的不足(numpy的底层是 ...
- ViewPager 源码分析(一) —— setAdapter() 与 populate()
写在前面 做安卓也有一定时间了,虽然常用控件都已大致掌握,然而随着 Android N 的发布,不自觉的愈发焦虑起来.说来惭愧,Android L 的 Material Design 库里的许多控件都 ...
- SteveY对Amazon和Google平台的吐槽
Steve Yegge, Amazon的前员工,现任Google员工,其本来想在Google+上和Google的员工讨论一些关于平台的东西,结果不小心把圈子设成了Public,结果这篇文章就公开给了全 ...
- 使用Openssl的AES加密算法
原文链接: http://blog.csdn.net/yasi_xi/article/details/13997337 Openssl是很常见的C接口的库,个人觉得易用.以下是AES加密的使用备忘.如 ...
- SharePoint 2013 实现多级审批工作流
上一篇介绍了安装和配置SharePoint 2013 Workflow,这一篇将用SharePoint 2013 Designer Workflow来实现一个多级审批工作流. 审批工作流介绍 这个De ...
- 转 部署Zipkin分布式性能追踪日志系统的操作记录
2017年02月27日 11:01:29 https://blog.csdn.net/konglongaa/article/details/58016398 阅读数:7631 Zipkin是Twitt ...
- Android Activity全面解析
Android Activity全面解析 首先,就从Android四大组件Activity开始. 1.Activity生命周期方法完全解析 activity_lifecycle.png 1).on ...
- 关于easyui combobox下拉框实现多选框的实现
好长时间没有更博了,一是因为最近真的比较忙,二是因为自己是真的偷懒了,哈哈 好啦,这篇博客主要是总结一些关于easyui combobox下拉框实现多选框的实现,包括前台界面的展示,和后台对数据的获取 ...
- python 取整的两种方法
问题简介: 要把一个浮点数(float)整数部分提取出来.比如把“2.1”变成“2”的这一过程:现在我们给这个过程起一个名字叫“取整”.那么它 在python中大致可以有两种写法 写法1)类型转换: ...