题目链接

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. 转:WebTest的常见问题与解决

    WebTest的常见问题与解决录制好一个WebTest,加上各种规则,编辑后运行并不会像我们想象的那么顺利成功,往往会碰到很多问题,运行不成功的情况比较多,这样我们就遇到了如何解决这些问题的情形.1. ...

  2. 写一个程序,统计自己C语言共写了多少行代码,Github基本操作

    前言 在上一篇博客中,本人提到了自己的文件操作可以说是几乎没用过.现在想想,这也算是只在OJ上做题的一个弊端吧.虽然通过OJ做题是一个学习代码好手段,但其他方面也要多多涉猎才好,而不是说OJ用不到文件 ...

  3. Allegro PCB -内层分割,比如电源层需要分割几种电源

    内层分割,比如电源层需要分割几种电源. (1).点击Display -> Assign Color 在Option中,先取一种颜色作为高亮显示的颜色. (2).在Find中,选Net,点击mor ...

  4. Windows下编译mxnet

    操作系统: 64位Windows10 mxnet的构建和安装包括两个步骤: 第一步,编译出共享库 libmxnet.dll. 第二步,安装语言包,比如python等. 最小编译要求: 最新的支持C++ ...

  5. PAT (Advanced Level) 1020. Tree Traversals (25)

    递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...

  6. Altera CYCLONE III FPGA BGA布线

    最近在做altera FPGA BGA相关的布线工作,收集了一些资料,公开出来以供大家讨论. 首先是器件引脚,只有弄清楚器件各个引脚的功能才能够进行布线,下面的文档详细描述了每个引脚的功能. 各引脚功 ...

  7. Lumen 时区设置

    根据 Laravel 4.x 和 5.0 的经验, 只需要到 config/app.php 中设置下 'timezone' 参数为 'PRC' 就好了, 找到 Lumen 的 config 目录, 在 ...

  8. AsParallel 用法

    http://www.cnblogs.com/leslies2/archive/2012/02/08/2320914.html AsParallel 通常想要实现并行查询,只需向数据源添加 AsPar ...

  9. hibernate---一对一单向主键关联(不重要)

    比如, husband的id参考wife的id husband.java: package com.bjsxt.hibernate; import javax.persistence.Entity; ...

  10. linux undelete

    http://www.tldp.org/HOWTO/archived/Ext2fs-Undeletion-Dir-Struct/index.html http://www.giis.co.in/deb ...