题目链接

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

题目思路:对于这个题目,可以先按速度的大小成小到大排序,

再成0 到 m ,把所有可以联通的道路全部暴搜一遍,一但联通,

两者的min=两者的速度差,依次成0  到 找 m  找,如果找到更最小的就取代min

最后的min一定是最小的,

看代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int father[222];
const int M=999999999;
int n,m;

struct node
{
int x,y,d;
} s[1111];

bool cmp(const node &a,const node &b)
{
return a.d<b.d;
}

int Find(int x)
{
if(x==father[x]) return x;
father[x]=Find(father[x]);
return father[x];
}

void Union(int x,int y)
{
x=Find(x);
y=Find(y);
if(x!=y)
{
father[x]=y;
}
}

int slove(int x,int y)
{
int min,max,i,j,k;
min=M;
for(i=0; i<m; i++)
{
for(j=1; j<=n; j++)
father[j]=j;
for(j=i; j<m; j++)
{
Union(s[j].x,s[j].y);
if(Find(x)==Find(y))//一旦两者联通,两者速度即相减
{
max=s[j].d-s[i].d;// 两者速度即相减。
if(max<min)
{
min=max;//取最小的差值。
break;
}
}
}
}
if(min==M) return -1;
else return min;
}

int main(void)
{
int i,j,k,l;
int x,y,q;
while(scanf("%d%d",&n,&m)==2)
{
for(i=0; i<m; i++)
scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].d);
sort(s,s+m,cmp);
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&x,&y);
printf("%d\n",slove(x,y));
}
}
return 0;
}

HDU-1598-find the most comfortable road(暴力+并查集)多看看,的更多相关文章

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

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

  2. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)

    一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...

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

    题意:略 分析:多询问问题,利用并查集加速.类似于kruskal对MST的构建:枚举最小的边,逐渐将更大的边加入集合,当查询的点在同一个集合,那么当前最小值,就是所加的最后一条边与第一条只差. 注意: ...

  4. HDU 1598 find the most comfortable road 并查集+贪心

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

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

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

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

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

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

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

  8. hdu 1598 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)

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

  10. HDU1598 find the most comfortable road 【并查集】+【枚举】

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

随机推荐

  1. openwrt串口的使用

    从 RT5350 的芯片手册上可以得知, RT5350 一共有两个串口, 分别为 UART Lite. UART Full, UART Lite 就是我们惯称为的串口 1,作为系统调试串口,通过这个串 ...

  2. 磁盘寻道时间算法之----------------SCAN算法和最短寻道时间优先调度算法

    若干个等待访问磁盘者依次要访问的柱面编号为:80,40,74,14,60,31,61,假设每移动一个柱面需要4毫秒时间,移动到当前位于35号柱面,且当前的移动方向向柱面号增加的方向.请计算: (1)若 ...

  3. 转:创建编码的WebTest

    创建编码的WebTest•通常,通过将现有的已记录Web测试转换为编码的Web测试来创建编码的Web测试.记录的Web测试以“Web测试编辑器”中可见的请求树开头.编码的Web测试是一个生成一系列We ...

  4. HDU 4421 ZOJ 3656 Bit Magic

    2-SAT,不要所有位置全部建好边再判断,那样会MLE的. 正解是,每一位建好边,就进行一次2-SAT. #include<cstdio> #include<cstring> ...

  5. 各种命令,以及FAQ..持续更新.....

    Linux 篇: CentOs 7 修改主机名 hostnamectl --static set-hostname <host-name> 统计最多的10条记录 awk '{print $ ...

  6. zepto学习之路--源代码提取

    最近在看zepto的源代码,把一些有用的函数摘出来,看看zepto是怎么实现的,自己做的时候也可以用.说实话,zepto的实现有一些看起来还是很晦涩的,可能是自己的水平不够,看不透作者的真正的意图. ...

  7. static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store); 的作用

    在 老罗的android例程里面有 static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store); /*读取设 ...

  8. iOS之Xcode修改应用图标

    随便找一个PNG为后缀的图标, 把它重命名为 icon.png 就可以了.  再次启动 IPhone 模拟器. 就成功了. 及时尺寸不符合审核规范,也是可以显示出来的,供测试效果吧. 为iPhone设 ...

  9. Python正则表达式,统计分析nginx访问日志

    目标: 1.正则表达式 2.oop编程,统计nginx访问日志中不同IP地址出现的次数并排序 1.正则表达式 #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  10. Linux如何正确关机

    其实,在Linux领域内大多用在服务器上,很少遇到关机的操作.毕竟服务器上跑一个服务是永无止境的,除非特殊情况下,不得已才会关机. Linux和windows不同,在 Linux 底下,由于每个程序( ...