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都对行驶 ...
随机推荐
- apt-key adv
gpg --recv-keys KEY-ID gpg --armor --export KEY-ID | sudo apt-key add - http://m.blog ...
- sql关键字过滤C#方法
/// <summary> ///SQL注入过滤 /// </summary> /// <param name="InText">要过滤的字符串 ...
- springmvc中的几个问题
一 url-pattern的问题: <!-- No mapping found for HTTP request with URI [/WEB-INF/jsp/homePage.jsp] i ...
- shell 空格问题
1.定义变量时, =号的两边不可以留空格. eg: gender=femal------------right gender =femal-----------wrong gender= femal- ...
- UIImageView 的contentMode属性 浅析
UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...
- Java中常见的5种WEB服务器介绍
这篇文章主要介绍了Java中常见的5种WEB服务器介绍,它们分别是Tomcat.Resin.JBoss.WebSphere.WebLogic,需要的朋友可以参考下 Web服务器是运行及发布Web应用的 ...
- 对于IE6版本图片透明。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- C# Socket的TCP通讯 异步 (2015-11-07 10:07:19)转载▼
异步 相对于同步,异步中的连接,接收和发送数据的方法都不一样,都有一个回调函数,就是即使不能连接或者接收不到数据,程序还是会一直执行下去,如果连接上了或者接到数据,程序会回到这个回调函数的地方重新往下 ...
- 图片处理中的Dithering技术
话说二战的时候,美国轰炸机每次执行任务,除了满载着威力强大的炸弹以外,还常常要装配一台计算机,飞机飞行方向和投弹的抛物线的计算都离不开这台机器.可是世界上第一台电子计算机在二战结束后才发明,轰炸机上当 ...
- PAT (Advanced Level) 1112. Stucked Keyboard (20)
找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include ...