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 ...
随机推荐
- k8s实战读书笔记
一.概述 kubernetes中Service是真实应用的抽象,将用来代理Pod,对外提供固定IP作为访问入口,这样通过访问Service便能访问到相应的Pod,而对访问者来说只需知道Service的 ...
- 【C语言】练习2-9
题目来源:<The C programming language>中的习题P38 练习2-9: 在求对二的补码时,表达式x &= (x-1)可以删除x中最右边值为1的一个二进 ...
- App开发准备
一. Android开发 二. IOS开发 1. 准备苹果电脑 Mac pro 一般比较贵,很少人或公司使用 替代的产品为 iMac 或 Mac mini 中配8G内存版 2. 准备苹果开发者账户,才 ...
- 创建多模块springcloud应用eureka server和client和消费端demo
使用环境是 STS + maven 1 创建父级 项目,springcloud-demo1 new -> maven project -> 按照要求进行配置即可.然后删除 src目录,因为 ...
- haproxy 非常完整的配置
常用配置选项: OPTION 选项: option httpclose :HAProxy会针对客户端的第一条请求的返回添加cookie并返回给客户端,客户端发送后续请求时会发送 此cookie到HAP ...
- Java数据结构和算法(十):二叉树
一.简介 二叉树是树这种数据结构的一员,后面我们还会介绍红黑树,2-3-4树等数据结构.那么为什么要使用树?它有什么优点? 前面我们介绍数组的数据结构,我们知道对于有序数组,查找很快,并介绍可以通过二 ...
- js两个日期相减
function dateHanle(d1,d2){ if(Date.parse(d1) - Date.parse(d2)==0) { console.log("d1等于d2"); ...
- hdu-2045 递归
#include <cstdio> #include <iostream> using namespace std; long long a[55] = {0,3,6}; lo ...
- Serizlizable
关闭 忧郁王子的专栏 伟大的意大利,伟大的罗伯特-巴乔 目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选 每周荐书:分布式.深度学习算法.iOS(评论送书) ...
- SQLMap 学习
注入完整流程:http://mp.weixin.qq.com/s/G_DUUVuPH9DeWagjELCPfA sqlmap命令:http://www.cnblogs.com/handt/p/855f ...