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都对行驶 ...
随机推荐
- <c:if>替代
由于没有else, 由下面的替代 <c:choose> <c:when test="${usersession.hasPrivilegeByName('Case Delet ...
- 报表Reporting S而vice是 错误的解决
Reporting Services 错误 报表服务器无法打开与报表服务器数据库的连接.所有请求和处理都要求与数据库建立连接. (rsReportServerDatabaseUnavailable)获 ...
- zf-分页后台代码
java : public ResultPage getDeptList(int page, int pageRows) throws Exception { String hql="fro ...
- document.querySelectorAll遍历(forEach小解)
document.querySelectorAll兼容性良好,在之前的项目中就其遍历方式出了错误,先做个小结: 1.for循环 传统遍历方法 for(var i= 0; i< document. ...
- JavaScript的第一次小结
一. JavaScript是一种的脚本语言:特点是:具有解释性,基于对象,事件驱动,安全性和跨平台等特点 对于这几种特点有必要说明一下 解释性:就是JavaScripte本身就是一种解释性语言 基于对 ...
- 从头到尾彻底解析Hash表算法
作者:July.wuliming.pkuoliver 说明:本文分为三部分内容, 第一部分为一道百度面试题Top K算法的详解:第二部分为关于Hash表算法的详细阐述:第三部分为打造一个最快的Hash ...
- HDU 5234 Happy birthday 动态规划(三维数组)
题目大意:过生日,有一个N*M的表格,每个位置都有一块一定重量的蛋糕你可以选择吃完或者不吃,从(1,1)走到(n,m),每次只能向右走或向下走,最多能吃k重量的蛋糕.问你最多能吃多少蛋糕. 题目思路: ...
- gameUnity 网络游戏框架
常常在想,有没有好的方式,让开发变得简单,让团队合作更加容易. 于是,某一天 动手写一个 架构, 目前版本 暂定 0.1 版本.(unity5.0.0f4 版本以上) 我打算 开源出来 0.1有什么功 ...
- java 创建一个File文件对象
Example10_1.java import java.io.*; public class Example10_1 { public static void main(String args[]) ...
- CodeForces 696A Lorenzo Von Matterhorn (LCA + map)
方法:求出最近公共祖先,使用map给他们计数,注意深度的求法. 代码如下: #include<iostream> #include<cstdio> #include<ma ...