题意:略

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

注意:当枚举的最小边,把所有大边加入都不能使查询点(a,b)加入同一集合,那么终止枚举。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAXN=;
const int INF=0x7fffffff; struct Edge{
int u,v,c;
Edge(){}
Edge(int _u,int _v,int _c):u(_u),v(_v),c(_c){}
}edge[MAXN]; int n,m;
int p[]; int cmp(Edge a,Edge b)
{
return a.c<b.c;
} void init()
{
for(int i=;i<=n;i++)
p[i]=i;
} int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
edge[i]=Edge(u,v,c);
}
sort(edge,edge+m,cmp); int q;
scanf("%d",&q);
while(q--)
{
int mi=INF,a,b;
scanf("%d%d",&a,&b);
for(int i=;i<m;i++)
{
init();
int j;
for(j=i;j<m;j++)
{
int u=edge[j].u;
int v=edge[j].v;
int x=find(u);
int y=find(v);
if(x!=y)
p[x]=y;
if(find(a)==find(b))
break;
}
if(j==m)
break;
mi=min(mi,edge[j].c-edge[i].c);
}
if(mi==INF)
printf("-1\n");
else
printf("%d\n",mi);
}
}
return ;
}

hdu 1598 find the most comfortable road(并查集)的更多相关文章

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

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

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

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

  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(枚举+卡鲁斯卡尔最小生成树)

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

  5. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

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

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

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

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

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

  8. HDU - 1598 find the most comfortable road 【最小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...

  9. HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合

    Problem Description XX星有很多城市,城市之间通过一种奇怪的快速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...

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

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

随机推荐

  1. 在sae中运行web.py应用

    sae 是新浪推出的PaaS业务,可以提供免运维的容器服务,官方网站( https://www.sinacloud.com/ ) 假设您已经在本地开发好了web.py 应用,您可以通过github客户 ...

  2. 使用Windows Live Writer开始发布cnblogs日志的方法

    1.下载Windows Live Writer http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=8621 2.安装Window ...

  3. windows上的docker容器内安装vim

    Reading package lists... Done Building dependency tree Reading state information... Done E: Unable t ...

  4. 谈谈 ES7、ES8、ES9 和 Stage 3 的那些事儿

    ES6 作为多年来 JavaScript 的重大版本变革,受到 JavaScript 开发者们的普遍欢迎. 也正是从 ES6 (ES2015) 开始,JavaScript 版本发布变为年更,即每年发布 ...

  5. 级联关系(内容大部分来自JavaEE轻量型解决方案其余的是我的想法)

    1. 级联关系 在Hibernate程序中持久化的对象之间会通过关联关系互相引用.对象进行保存.更新和删除等操作时,有时需要被关联的对象也执行相应的操作,如:假设需要关联关系的主动方对象执行操作时,被 ...

  6. WIN7无法卸载掉中文繁体注音输入法

    WIN7无法卸载掉中文繁体注音输入法 不知何时系统里被自动安装了个中文繁体的注音输入法,每次启动都会替换默认的简体搜狗拼音,而且最要命的是在输入法选择栏里面没有出现这个繁体的输入法,而任务栏里却总是有 ...

  7. python移植性提示

    移植性提示 2.1 预计在python3.0中,运算符/只能执行True除法.3.0发布后,程序员要更新自己的程序,以兼容新行为. 3.1 python2.0版本开始引入增量赋值符号,在老版本pyth ...

  8. sqoop使用记录

    sqoop简介 Sqoop是用来实现结构型数据(如关系数据库)和Hadoop之间进行数据迁移的工具.它充分利用了MapReduce的并行特点以批处理的方式加快数据的传输,同时也借助MapReduce实 ...

  9. zoj 3882 Help Bob(zoj 2015年7月月赛)

    Help Bob Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a game very popular in ZJU at pr ...

  10. android layout 布局属性

    控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或false android:layout ...