HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用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(枚举+并查集,类似于最小生成树)的更多相关文章
- hdu 1598 find the most comfortable road(并查集+枚举)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 1598 find the most comfortable road(并查集)
题意:略 分析:多询问问题,利用并查集加速.类似于kruskal对MST的构建:枚举最小的边,逐渐将更大的边加入集合,当查询的点在同一个集合,那么当前最小值,就是所加的最后一条边与第一条只差. 注意: ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- HDU 1598 find the most comfortable road (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 1598 find the most comfortable road (并查集)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU1598 find the most comfortable road 【并查集】+【枚举】
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
随机推荐
- Request.IsLocal与Request.Url.IsLoopback的区别
均在服务器上访问时: http://localhost:17810 Request.IsLocal => trueRequest.Url.IsLoopback => true http:/ ...
- Linux 网络故障排查
1.第一步是要确认网卡本身是否工作正常?利用ping工具可以确认这点.输入ping 127.0.0.1 ,然后看是否正常ping 通? 这里的127.0.0.1 被称作主机的回环接口,是TCP/IP协 ...
- linux系统目录架构
/bin目录:可执行的二进制文件,shell命令(就是我们说的命令:cp ls ...),所有用户都有权执行. /boot目录:引导目录,整个操作系统启动所需的所有文件都在该目录下,其中最主要的就是v ...
- 设置datagridview中button按钮的背景颜色
问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...
- 通信协议之HTTP,UDP,TCP协议
1.UDP,TCP,HTTP之间的关系 tcp/ip是个协议组,它可以分为4个层次,即网路接口层,网络层,传输层,以及应用层, 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协 ...
- 解决nginx负载均衡的session共享问题
1.不使用session,换用cookie session是存放在服务器端的,cookie是存放在客户端的,我们可以把用户访问页面产生的session放到cookie里面,就是以cookie为中转站. ...
- TOMCAT内存大小调整
Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个java虚拟机.JAVA程序启动时JVM都会分配一个初始内存和最大内存给这个应用程序.这个初始内存和最大内存在一定程度都会 ...
- jasonTree多选多级树控件
jasonTree1.0 jasonTree多选多级树控件(名字是自己取),用于友好的展示树形结构的数据,并可以多选,传统的做法是在一个select的下拉框中显示一个可折叠的树结构,公司的需求人员这种 ...
- 关于使用视图进行分页时出现当前记录集不支持书签的错误解决方法及原因(asp)
一般在使用视图进行查询时,视图中意般都关联了两个或者更多个表,一般在这种情况下才会使用视图,但是但我在使用视图来查询数据时没有问题,但是一旦在分页中使用到视图进行查询就会出现错误提示如下: ADODB ...
- Bash 快捷键
生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向 ...