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 ...
随机推荐
- HDU 1258 Sum It Up (DFS)
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- php sockent通信
1.php服务端:server.php <?php //确保在连接客户端时不会超时 set_time_limit(0); $ip = '127.0.0.1'; $port = 1935; /* ...
- iOS 特定时间内才做某件事,有类似奇葩需求可以参考
我们项目启动的时候要弹出一个广告窗口,很简单的一个功能,服务器的判断一下满足条件,即返回数据,客户端判断数据部位NULL,则弹出弹窗但是老板说,这个要时间短弹出,每天的中午12点到下午2点不能弹出来这 ...
- 【struts2】核心概念
1)FilterDispatcher Struts2的前端控制器,也是Struts2的MVC中的控制器部分.在实际开发中,只需要在web.xml中配置一次即可,如下所示: <filter> ...
- MySQL子查询的优化
本文基于MySQL5.7.19测试 创建四张表,pt1.pt2表加上主键 mysql> create table t1 (a1 int, b1 int); mysql> create ta ...
- 解决Mac上adb: command not found问题
使用mac进行开发的时候,有时候需要使用adb指令来进行一些操作,但是如果没有配置过Android环境变量,可能会出现adb: command not found的问题,查了一些资料,这里记录一下ma ...
- 让windows能像ubuntu一样方便的神器
让windows能像ubuntu一样方便的神器: Let's get Chocolatey! Chocolatey NuGet is a Machine Package Manager, somewh ...
- Groovy 学习手册(6)
9. 不可变特性 不可变特性和函数式编程在一起就像是花生酱和果酱在一起一样.虽然没有必要非要在一起使用,但他们相处得很好. 在纯正的函数式语言中,每个函数对本身之外没有影响,即没有副作用.这意味着每次 ...
- HighCharts实现多数据折线图分列显示
HighCharts实现多数据折线图分列显示 BY ZYZ HighCharts是一个很好用的web端绘图插件,用起来很方便,它的官方支持很好.并且有中文API(不全然).画出来的图像也挺美丽的. 近 ...
- 常用的web安全处理
1:用户名和密码认证通过后,必须更换会话标识,以防止会话固定(session fixation)漏洞. 实施指导: 在用户名和密码认证通过后增加以下两行代码: request.getSession() ...