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 ...
随机推荐
- Android四大组件之一:Activity
介绍:活动是最基本的Android组件之一,在应用程序中,一个活动通常就是一个用户界面,每一个活动都被实现为一个独立的类,并且从活动几类中继承, 活动类将会显示由View控件组成的用户接口,并对时间E ...
- Different ways to invoke a shared object/share library(.so)
在Linux中调用.so文件的方法有几种. 1.直接在编译的时候链接上. 2.dlopen/???
- AOJ 2200 Mr. Rito Post Office
Mr. Rito Post Office Time Limit : 8 sec, Memory Limit : 65536 KB Problem D: Mr. Rito Post Office あなた ...
- sql临时表和表变量
1. 为什么要使用表变量 表变量是从2000开始引入的,微软认为与本地临时表相比,表变量具有如下优点: a.与其他变量的定义一样,表变量具有良好的定义范围,并会被自动清除: b.在存储过程中使用表 ...
- linux中的文件属性
l 是链接d 是目录c 是字符设备文件b 是块设备- 是文件
- 转:java中volatile关键字的含义
转:java中volatile关键字的含义 在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言 ...
- php生成圆形图片
http://files.cnblogs.com/files/adtuu/circle_image.zip
- curl 报错记录,mark
今天在做接口开发的时候,使用curl post ,请求返回数据为 null ,很纳闷,然后使用 curl_errno 打印出来的错误代码为 28 ,curl_error($ch) 打印出来的是Oper ...
- 【转载】Powershell连接世纪互联Office365
$User = "admin@contoso.com" $PWord = ConvertTo-SecureString –String "password" – ...
- EWOULDBLOCK = EAGAIN
#define EAGAIN 11 /* Try again */ #define EINTR 4 /* Interrupted system call */ #define EWOULDBLOCK ...