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都对行驶 ...
随机推荐
- rebar
www.cnblogs.com/panfeng412/archive/2011/08/14/2137990.html
- C#生成随机汉字
using System; using System.Text; namespace ConsoleApplication { class ChineseCode { ...
- white-space详解
white-space共有5种属性normal,nowrap,pre,pre-wrap,pre-line 网上的解释多半过于详细冗长,先做个简化处理,以便查询 normal 忽略空白 过长换行 ...
- windows MySQL 安装
MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C:\P ...
- UVA 11027 - Palindromic Permutation
题目意思为解码字符串,要输出第n个回文字符串,因为对称关系,前一半确定了,后一半也就跟着确定了,所以n其实就是前一半字符串的编码,还要减去1,直接解码出来再复制给后半即可 #include<cs ...
- 【转】VC6.0打开或者添加工程文件崩溃的解决方法
很多学习编程的同学都遇到这样的问题,在Windows操作系统下使用Visual C++ 6.0编程时,如果点击菜单中的[打开]或者[添加],或者按快捷键,都会弹出下图的对话框,出现程序崩溃并退出的情况 ...
- skia入门
SkBitmap bmp; bmp.setConfig(SkBitmap::kARGB_8888_Config, rect.Width(), rect.Height()); bmp.allocPixe ...
- jave学习1--基础介绍
java 技术主要分为三个部分: jave SE基础知识. 对于各个程序的开发语言都包含的基本数据类型,循环控制,数组,方法等. jave SE的面向对象部分. 所有的面向对象的概念,为最终的接口准备 ...
- Webdriver+testNG+ReportNG+Maven+SVN+Jenkins自动化测试框架的pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- iOS进阶推荐的书目
<Effective Objective-C 2.0:编写高质量iOS与OS X代码的52个有效方法>([英]Matt Galloway) 很多面试题有涉及 <IOS数据库应用高级编 ...