题意:略

分析:多询问问题,利用并查集加速。类似于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. MySql笔记之修改数据库编码

    修改前(系统默认编码) 查看编码命令: show variables like '%char%'; show variables like 'character%'; 两种方法 a) 暂时性修改 SE ...

  2. 洛谷——P1866 编号

    P1866 编号 题目描述 太郎有N只兔子,现在为了方便识别它们,太郎要给他们编号.兔子们向太郎表达了它们对号码的喜好,每个兔子i想要一个整数,介于1和Maxnumber[i]之间(包括1和Maxnu ...

  3. 洛谷 ——2925干草出售Hay For Sale

    题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...

  4. [BZOJ5288][HNOI2018]游戏(拓扑排序)

    传送门:https://www.luogu.org/problemnew/show/P4436 20分的暴力加一个Random_shuffle就A了.我还能说什么.. 不过这个也不是毫无道理,复杂度应 ...

  5. ARM常用汇编指令介绍

    b     跳转指令(跳转范围为32Mb) bl    带返回地址的跳转,指令自动将下一条指令的地址复制到R14寄存器,然后跳转到指定地址去执行,执行完后返回到下一条指令处执行 pc    寄存器R1 ...

  6. 删除VisualStudio 2013中的 "send Feedback" 按钮

    在VisualStudio 2013中,在标题栏中增加了一个 "send Feedback" 按钮,用于给微软发送Bug和回馈(或者一个哭脸和笑脸).这个按钮对于开发来说基本上没用 ...

  7. System.InvalidOperationException异常

    Q:捕捉到 System.InvalidOperationException Message=集合已修改:可能无法执行枚举操作. A:解决方法:利用for进行遍历. 原因: 1.hashtable存放 ...

  8. 能上架App的GooglePlay开发者账号获取流程

    googleplay 开发者账号申请流程 接到公司号召,要让我们的app走向世界,上架GooglePlay,都说天朝的Android 程序员是折翼的天使,猛然发现写了做么多年的Android,竟然不知 ...

  9. JDBCUtils工具类

    JdbcUtils.java import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource ...

  10. (转)指针的引用(*&)与指针的指针(**)

    本文转载而来,转载出处:http://www.cppblog.com/doing5552/archive/2010/09/28/127994.html 在下列函数声明中,为什么要同时使用*和& ...