题意:略

分析:多询问问题,利用并查集加速。类似于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. HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)

    Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tot ...

  2. JDBC二部曲之_入门

    JDBC 1 什么是JDBC? JDBC(Java DataBase Connectivity),即Java数据库连接!也就是说,Java程序员可以使用JDBC API来操作数据库. 最早JDBC是J ...

  3. 每天一个liunx命令4之 ps -ef ,ps -aux ,ps aux

    1ps aux和ps –aux 请注意"ps -aux"不同于"ps aux".POSIX和UNIX的标准要求"ps -aux"打印用户名为 ...

  4. 在mac上运行android的intel模拟器导致的死机问题解决

    最近用Mac开发android的时候遇到了一个烦人的问题,装上了HAXM,intel hardware accelerated execution manager以后,一运行android emula ...

  5. Android可伸缩列表ExpandableListView

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  6. tomcat下载安装以及在eclipse中的配置

    eclipse的下载地址是http://www.eclipse.org/downloads/. tomcat的下载地址为http://tomcat.apache.org/ 这两个工具的安装都非常eas ...

  7. 让Mac支持lrzsz

    http://blog.csdn.net/citywolf4/article/details/49071679 https://github.com/mmastrac/iterm2-zmodem

  8. wget jdk

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...

  9. JS或jQuery获取当前屏幕宽度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  10. [Tools] Deploy a Monorepo to Now V2

    Now by Zeit has recently been updated and now supports multi-language monorepos. In this lesson we'l ...