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 ...
随机推荐
- iOS 非ARC基本内存管理系列总结6 -设计微博模型
设计简单的微博模型:用User类和Status类来模拟实现 在非ARC机制下有两种方式,两者没有太大的区别之所以写了两种只是为了方便学习和对比两种写法! 第一种:没有使用atuorelease和自动释 ...
- 如何去掉html中的超链接
$a= preg_replace("/<a[^>]+>/", "", $a); $a= preg_replace("/<\/a ...
- 安装360后,visual studio 经常报各种莫名其妙的错误的解决方案
安装360后,visual studio 经常报各种莫名其妙的错误,每次都要查找错误的解决方案 而且网上关于这个的好少,以后只要碰到了这种情况我就记录下吧 今天碰到的情况是打开WCF服务时出现 ...
- Visual Assist X破解方法
VC2008的破解方法:使用2008的朋友我就不多说了,直接拷贝到你选择的那个安装目录去,例如 C:\Program Files\Visual Assist\ ,直接运覆盖VA_X.dll 即可VC2 ...
- Lua基础之字符串(string)
1,计算字符串长度 2,返回字符串s的n个拷贝 3,返回字符串全部字母大写 4,返回字符串全部字母小写 5,返回一个类似printf的格式化字符串 6,根据下标截取字符串 7,在字符串中查找 8,在字 ...
- spring mvc的excel报表文件下载时流的冲突解决
在jsp或者在servlet中有时要用到 response.getOutputStream(),但是此时会在后台报这个错误java.lang.IllegalStateException: getOut ...
- C语言标准库函数strcpy与strcmp的简单实现
//C语言标准库函数strcpy的一种简单实现. //返回值:目标串的地址. //对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL. //参数:des为目标字符串, ...
- 面试问到:JDBC、hibernate、ibati
一.JDBC.Connection(连接) 优点:运行高效.快捷. 缺点:代码多.异常多.不支持跨平台. 二.ibatis 1.根据jdbc的基本建立连接. 2.通过anntation+xml.jav ...
- linux消息队列的使用
消息队列 *消息队列是内核地址空间中的内部链表,通过内核在各个进程之间传递的内容.消息顺序发送到消息队列中,每个消息队列都有IPC标识符唯一地进行标识. msgbuf结构 struct msgbuf{ ...
- hibernate中增加annotation @后不提示信息【转】
此文转自:http://blog.knowsky.com/252047.htm 所需要用到的3个jar包分别是: hibernate-annotations.jar ejb3-persistence. ...