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 ...
随机推荐
- POJ 3624
背包问题,在定容量的背包中放入物体求装满价值最大.因为每种物体数量只有1,故只有放与不放. #include<iostream> #include<cstring> #incl ...
- python——异常处理
python处理异常的格式一般如下: try: A部分 Except .... [as ..]: B部分 [ finally: C部分 Else: D部分 ] ps: 1.不管有没有异常,final ...
- oc常见误区
1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作, 2.异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然 ...
- 转:Android studio Gradle
提高Android Studio中Gradle执行效率 分类: android studio2015-06-26 11:54 2374人阅读 评论(2) 收藏 举报 android studiogra ...
- 简单的下拉刷新以及优化--SwipeRefreshLayout
代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,Swipe ...
- 如果在配置中将“system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled”设置为 true,则需要终结点指定相对地址。如果在终结点上指定相对侦听 URI,则该地址可以是绝对地址。若要解决此问题,请为终结点“http://localhost/Service1.svc”指定相对 URI。
问题: 如果在配置中将"system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled"设置为 ...
- 如何保持iOS上键盘出现时输入框不被覆盖
在 iOS5 上请求显示键盘时,系统从屏幕底部将键盘滑入上来,位于应用的内容之上. (墙内:http://mikixiyou.iteye.com/blog/1488302) 如果屏幕中的内容项目比较多 ...
- win下php5.5.12装不上memcache扩展
WAMP这个集成环境里,php目录下有个php.ini,apache/bin下也有一个php.ini,环境使用的是apache下的,改apache
- 深入理解用mysql_fetch_row()以数组的形式返回查询结果
同mysql_result()一样,mysql_fetch_row()也可以用来获取查询结果集,其区别在于函数的返回值不是一个字符串,而是一个数组.函数定义如下. 复制代码 代码如下: array m ...
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
C. Wilbur and Points time limit per test 2 seconds memory limit per test 256 megabytes input standar ...