一开始想到用BFS,写了之后,发现有点不太行。网上查了一下别人的解法。

首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start、end属于同一个集合,即可以连通时停止。
过程类似于最小生成树。舒适感即为选取的最后一条边减去一开始的那条边,所有情况取最小值。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <set>
#include <vector>
#include <queue>
#include <algorithm> using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
const int maxm=;
int n,m,q,ans,s,t; struct Edge{
int u,v,speed; bool operator < (const Edge tmp) const{
return speed<tmp.speed;
}
}edge[maxm]; struct UF{
int father[maxn];
void init(){
for(int i=;i<=n;i++){
father[i]=i;
}
} int find_root(int x){
if(father[x]!=x)
father[x]=find_root(father[x]);
return father[x];
} void Union(int a,int b){
int x=find_root(a);
int y=find_root(b);
if(x==y)
return;
father[y]=x;
}
}uf; int main()
{
int a,b,c;
int u,v;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
edge[i].speed=c;
edge[i].u=a;
edge[i].v=b;
}
sort(edge+,edge+m+);
scanf("%d",&q);
for(int w=;w<=q;w++){
int i,j;
ans=INF;
scanf("%d%d",&s,&t);
//开始从最小边枚举
for(i=;i<=m;i++){
uf.init();
//每次取比它大的边
for(j=i;j<=m;j++){
u=edge[j].u;
v=edge[j].v;
uf.Union(u,v);
int x=uf.find_root(s);
int y=uf.find_root(t);
//若s、t再同一个集合,表明它们之间已构成联通路
if(x==y){
break;
}
}
//取完后面所有边,但s、t仍不能连通
if(j>m)
continue;
//更新最小值
if(edge[j].speed-edge[i].speed<ans)
ans=edge[j].speed-edge[i].speed;
}
if(ans<INF)
printf("%d\n",ans);
else
printf("-1\n");
}
}
return ;
}

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(并查集)

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

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

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

  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(最小生成树之Kruskal)

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

  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. HDU1598 find the most comfortable road 【并查集】+【枚举】

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

随机推荐

  1. Change http port in bitnami stack

    My case goes like this. I installed bitnami redmine first with port 80 for http service, but got pro ...

  2. Java线程角度的内存模型和volatile型变量

    内存模型的目标是定义程序中各个变量的访问 规则,即在虚拟机中将变量(包括实例字段,静态字段和构成数组对象的元素,不包括局部变量与方法参数,因为后者是线程私有的)存储到内存和从内存中取出变量这样的底层细 ...

  3. DEDECMS中,会员中心的常用知识

    会员中心 引入了member/config.php,即可用$cfg_ml->fields['face'].$cfg_ml->fields['spacesta']等

  4. 修改ubuntu按下关机键触发的事件

    gsettings set org.gnome.settings-daemon.plugins.power button-power shutdown will change your the beh ...

  5. Lucene使用IKAnalyzer分词实例 及 IKAnalyzer扩展词库

    文章转载自:http://www.cnblogs.com/dennisit/archive/2013/04/07/3005847.html 方案一: 基于配置的词典扩充 项目结构图如下: IK分词器还 ...

  6. 玄机宝盒v1.6.1.1

    最新版本:玄机宝盒v1.6.1.1 玄机宝盒v1.6.1.1 04-14/2016 给你的将是无与伦比的体验http://bbs.msdn5.com/thread-15-1-1.html(出处: 玄机 ...

  7. DOS环境下含包并引用第三方jar的java程序的编译及运行

    DOS环境下含包并引用第三方jar的java程序的编译及运行 1.程序目录机构 bin:class文件生成目录 lib:第三方jar包目录 src:源程序文件目录 2.程序代码: 3.程序编译 jav ...

  8. sublime3 注册码

    —– BEGIN LICENSE —– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 AFF6F6 ...

  9. Android Material Design:滑动指示选项卡android.support.design.widget.TabLayout的简单使用

    该TabLayout的功用,简单的说,就是当用户在该TabLayout的选项卡子item中选择触摸时候,文字和下方的指示器横条滑动指示.这个功能就是以前APP开发常用的选项卡某一卡片被切换.选中时候的 ...

  10. Global::validateEmail

    /***************************************************************** (C) Copyright DENTSPLY Internatio ...