HDU 1589 Find The Most Comfortable Road 最小生成树+枚举
find the most comfortable road
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
【Sample Output】
【题意】
给定一张无向有权图和一些询问,每一个询问都是一对起/终点,对于每一个询问,要求找到一条路能从起点到达终点,并且得到该条路上所有边权值中最大边与最小边的差,使得这个差值达到最小。最终的输出结果是这个最小差值。
【分析】
考虑Kruskal的贪心过程:将边从小到大排序,不断添边的过程中用并查集判断端点的归属情况。
假设在MST的寻找过程中,一对询问的其中一个点已经加入集合,当找到另外一个点加入集合的时刻寻找就可以结束,此时能够保证最后这条加入的边是已有的边中最大的,因为更大的边还在后面。
所以可以不断枚举最小边,以指定的最小边为基础进行Kruskal最小生成树操作,这里可能有两种情况:
1、最小边恰好在起/终点的路径上,则找到的最后一条边与最小边的差值即为这次查找的结果;
2、最小边不在起/终点的路径上,没有关系,因为后序枚举中仍然能够找出来。
因为使用了贪心性质,这里不能保证这个算法是最优解,但是可以保证结果的正确性。
#include<iostream>
#include<cstdio>
#include<algorithm> using namespace std; typedef struct {
int a,b,c;
} node;
node a[]; bool op(node a,node b)
{
return a.c<b.c;
} int father[]; void clean_father(int n)
{
for (int i=;i<=n;i++) father[i]=i;
} int getfather(int x)
{
if (father[x]!=x) father[x]=getfather(father[x]);
return father[x];
} void link(int x,int y)
{
father[getfather(x)]=getfather(y);
} int main()
{
int n,m;
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i=;i<=m;i++) scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c);
sort(&a[],&a[m+],op); int q;
scanf("%d",&q);
for (int i=;i<=q;i++)
{
int t1,t2;
scanf("%d%d",&t1,&t2); int minn,maxn,ans=;
for (int j=;j<=m;j++)
{
minn=;
maxn=;
clean_father(n);
for (int k=j;k<=m;k++)
if (getfather(a[k].a)!=getfather(a[k].b))
{
link(a[k].a,a[k].b);
if (minn>a[k].c) minn=a[k].c;
if (maxn<a[k].c) maxn=a[k].c;
if (maxn-minn>ans) break;
if (getfather(t1)==getfather(t2))
{
if (ans>maxn-minn)
{
ans=maxn-minn;
break;
}
}
}
} if (ans!=) printf("%d\n",ans);
else printf("-1\n");
}
} return ;
}
HDU 1589 Find The Most Comfortable Road 最小生成树+枚举的更多相关文章
- 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(枚举+卡鲁斯卡尔最小生成树)
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 (并查集)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 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 ...
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
- HDU 1598 find the most comfortable road (最小生成树) >>
Problem Description XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...
随机推荐
- 导航条css实现和table实现
导航条式样 <style type="text/css"> ul,li{ margin:0; padding:0; list-style:none; } #navtop ...
- mysql日志详细解析【转载】
转自:http://pangge.blog.51cto.com/6013757/1319304 MySQL日志: 主要包含:错误日志.查询日志.慢查询日志.事务日志.二进制日志: 日志是mysql数据 ...
- ActiveX控件打包成Cab实现浏览器自动下载安装
前言 我们在浏览器中使用我们自己的一些OCX,或者是DLL这一类的文件,在X86的机器上需要我们手动将这些文件拷贝到Windows/System32 文件夹下面去,然后通过Dos命令regsvr32 ...
- think in uml-关系
1.关联关系association 在一段时间内将多个类的实例连接在一起 某个对象在一段时间内一直"知道"另一个对象的存在 2.依赖关系dependency 一个对象的修改会导致另 ...
- 第一次安装ubuntu要设置的东西
1. 安装网卡驱动 lscpi 查看网卡型号 根据型号找到驱动源码 下载下来并编译 安装 2. 编译安卓源码的时候出现jdk型号不对的情况 把/usr/bin/java 删除,就可以了.
- photoshop移动工具
1*移动工具 V 移动图层 若果移动选区相当于剪切 2*
- dos cmd重启2003命令shutdown -r -t 0
用cmd命令来关闭或重启电脑. at 2:00 /every:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday shutdown /r ...
- Python定制类
https://docs.python.org/3/reference/datamodel.html#special-method-names
- oracle_一次移动数据库dbf文件的操作
oracle数据库的dbf路径下面磁盘不足,需要把原始路径下面的dbf文件移动到另外一个磁盘路径下, 具体的操作有四步. 1.把整个表空间offline. 2.copy原始路径下的dbf文件到新的路径 ...
- android 中ImageButton按下改变背景图片的效果
最近在做一个app的登陆界面,才发现原来认为很简单的UI效果,其实背后却蕴含的知识很多,积累一个算一个吧. 实现方法有两种:一种是添加代码,一种是配置xml文件. 方法一:代码添加 ImageButt ...