最小瓶颈路 Uva 534 Frogger
说明:关于Uva的题目,可以在vjudge上做的,不用到Uva(那个极其慢的)网站去做。
最小瓶颈路:找u到v的一条路径满足最大边权值尽量小
先求最小生成树,然后u到v的路径在树上是唯一的,答案就是这条路径。
Uva 534 Frogger
Time Limit: 3000MS 64bit IO Format: %lld & %llu
Description
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping. Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps. To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence. The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones. You are given the coordinates of Freddy’s stone, Fiona’s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy’s and Fiona’s stone.
Input
The input file will contain one or more test cases. The first line of each test case will contain the number of stones n (2 ≤ n ≤ 200). The next n lines each contain two integers xi , yi (0 ≤ xi , yi ≤ 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n − 2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying ‘Scenario #x’ and a line saying ‘Frog Distance = y’ where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
Sample Input
2
0 0
3 4
3
17 4
19 4
18 5
0
Sample Output
Scenario #1
Frog Distance = 5.000
Scenario #2
Frog Distance = 1.414
/*-------------------------------------------------*/
解析:
原来的想法是先求最小生成树,然后倍增求出答案。这样虽然可以但是比较麻烦。对于要求U--V最小瓶颈路的总长度,这是最快的方法。 介于这道题是单对询问路上最大边的最小值,我们可以找到更简单的做法:郭华阳在国家集训队论文里介绍了最小生成树的性质。就是在kruskal算法执行的时候第一次将两个点(或者说两个点的集合)连起来的那条边就是这两点的最小瓶颈路上最大边。(因为kruskal是从小到大依次连边的。)一旦明白了让这条性质这题就变得简单多了。
#include<iostream>
using namespace std;
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define N 205
struct Edge{
int u,v;
double w;
}edge[N*N];
int n,t;
struct D{
double x,y;
}dian[N];
int father[N];
void add_edge(int a,int b)
{
++t;
edge[t].u=a;
edge[t].v=b;
edge[t].w=sqrt((dian[a].x-dian[b].x)*(dian[a].x-dian[b].x)+(dian[a].y-dian[b].y)*(dian[a].y-dian[b].y));
}
bool cmp(Edge P,Edge Q)
{
return P.w<Q.w;
}
int find(int x)
{
return (father[x]==x?x:father[x]=find(father[x]));
}
void kruskal(double &ans)
{
for(int i=;i<=n;++i)
father[i]=i;
sort(edge+,edge+t+,cmp);
for(int l=;l<=t;++l)
{
int f1=find(edge[l].u);
int f2=find(edge[l].v);
if(f1==f2) continue;
father[f2]=f1;
if(find()==find()) /*1是起点,2是终点,在kruskal算法执行的时候第一次将两个点(或者说两个点的集合)连起来的那条边就是这两点的最小瓶颈路上最大边。*/
{
ans=edge[l].w;/*这个性质由kruskal算法的过程即可知道*/
return;
}
}
}
int main()
{
int topt=;
while(scanf("%d",&n)==)
{
topt++;
if(n==) break;
t=;
for(int i=;i<=n;++i)
{
scanf("%lf%lf",&dian[i].x,&dian[i].y);
if(i==)continue;
for(int j=;j<i;++j)
add_edge(j,i);
}
double ans;
kruskal(ans);
printf("Scenario #%d\n",topt);
printf("Frog Distance = %0.3lf\n\n",ans);
}
return ;
}
最小瓶颈路 Uva 534 Frogger的更多相关文章
- 【uva 534】Frogger(图论--最小瓶颈路 模版题)
题意:平面上有N个石头,给出坐标.一只青蛙从1号石头跳到2号石头,使路径上的最长便最短.输出这个值.(2≤N≤200) 解法:最小瓶颈树.而由于这题N比较小便可以用2种方法:1.最短路径中提到过的Fl ...
- UVA 11354 Bond(最小瓶颈路+倍增)
题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...
- 【UVA534】Frogger 最小瓶颈路
题目大意:给定一张 N 个点的完全图,求 1,2 号节点之间的一条最小瓶颈路. 题解:可知,最小瓶颈路一定存在于最小生成树(最小瓶颈树)中.因此,直接跑克鲁斯卡尔算法,当 1,2 号节点在同一个联通块 ...
- UVa 11354 邦德(最小瓶颈路+LCA)
https://vjudge.net/problem/UVA-11354 题意: 有n个城市m条道路,每条道路有一个危险系数.先在有若干个询问,要求找到一条从s到t的路,使得途径所有边的最大危险系数最 ...
- UVA 534 - Frogger(kruskal扩展)
UVA 534 - Frogger 题目链接 题意:给定一些点.如今要求一条路径从第一个点能跳到第二个点,而且这个路径上的最大距离是最小的 思路:利用kruskal算法,每次加最小权值的边进去,推断一 ...
- POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径)
POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径) Description Freddy Frog is sitting on ...
- UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)
题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...
- 【20181102T2】飞越行星带【智商题+最小瓶颈路】
题面 [正解] 一眼不可做啊 --相当于求路线上穿过的点最小距离最大 最小最大--二分啊 现在相当于给一个直径,要判断这个直径是否能从左边穿到右边 我们可以在距离不超过直径的点连一条边,\(y=0\) ...
- 【UVA10816】Travel in Desert (最小瓶颈路+最短路)
UVA10816 Travel in Desert 题目大意 沙漠中有一些道路,每个道路有一个温度和距离,要求s,t两点间的一条路径,满足温度最大值最小,并且长度最短 输入格式 输入包含多组数据. 每 ...
随机推荐
- PHP学习笔记:通过curl实现采集网站内容
关于curl,请各位同学自行百度,我直接上案例. 首先开启你的curl拓展,在php.ini文件把curl拓展开启,即取消extension=php_curl.dll的分号. eg:利用curl采集网 ...
- pygame for python3.3
pygame的更新慢的令人发指,我最初使用的python是3.4版本的,无何奈何pygame不支持3.4,甚至官网只有3.2版本的.我于是将各种版本试了一遍,出现各种问题,同时我比较钟爱3.x版本,最 ...
- 自定义View_2_关于自定义组合View
自定义View(2) Android当中给我们提供了丰富的UI控件,当然也许满足不了我们的需求,我们就必须学会自定义自己的View,我们怎么算是自定义自己的view呢! 我们会根据原来有的View对V ...
- linux多线程-互斥&条件变量与同步
多线程代码问题描述 我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位.在多线程的程序中,多个线程共享临界区资源,那么就会有问题: 比如 #include < ...
- Mac OSX 下用 Homebrew 安装 MongoDB 并配置到 WebStorm 中
1. 安装 Ruby OSX 操作系统内置 Ruby,但如果没有 Ruby,则需先输入以下命令安装能够进行多版本ruby环境安装.管理和切换的命令行工具 RVM. 1.1 安装 RVM 打开终端输入以 ...
- JSON的三种解析方式
一.什么是JSON? JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度. JSON就是一串字符串 只不过元素会使用特定 ...
- IE浏览器中ajax使用缓存数据的问题
今天做了一个小功能:点击鼠标实时更新系统时间,采用ajax,过程很顺利,没遇到啥差错,谷歌,火狐,欧鹏一律通过,怀着忐忑的心情点开了IE8,果然,IE要对得起前端杀手的称号:更新不了时间. 查了一下这 ...
- CKEditor与CKFinder的配置
CKEditor与CKFinder的配置使用(一) 将CKEditor 与 CKFinder 的包含在项目中 从http://cksource.com网站上下载CKEditor与CKFinder,并将 ...
- Mybatis学习记录(七)----Mybatis查询缓存
1. 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存.在操作数据库时需要构造 sql ...
- STL--双端队列(deque)和链表(list)
双端队列(deque容器类): #include<deque>与vector 类似,支持随机访问和快速插入删除,它在容器中某一位置上的操作所花费的是线性时间. 与vector不同的是:de ...