题意

题目主要说的是,有两只青蛙,在两个石头上,他们之间也有一些石头,一只青蛙要想到达另一只青蛙所在地方,必须跳在石头上。题目中给出了两只青蛙的初始位置,以及剩余石头的位置,问一只青蛙到达另一只青蛙所在地的所有路径中的“the frog distance”中的最小值。

解释一下“the frog distance”:题目中给出了一段解释“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.” 其中 jump range 实际上就是指一条通路上的最大边,该词前面的minimum就说明了要求所有通路中最大边中的最小边。如果直接说前面这句话你可能感觉比较绕,通过上面的解释后我想你应该明白了吧。

通过上面的分析,不难看出这道题目的是求所有通路中最大边中的最小边,可以通过利用floyd,Dijkstra算法解决该题目,注意这道题可不是让你求两个点之间的最短路的,只不过用到了其中的一些算法思想。当然如果用Dijkstr算法解决该题需要一个特别重要的方程,即

d[j] = min(d[j], max(d[x], dist[x] [j])); //dis[j]为从1号石头到第j号石头所有通路中最长边中的最小边.

如果用floyd算法解决该题则用到方程

dist[i][j] = min(dist[i][j], max(dist[i][k], dist[k][j]));

AC代码

利用Dijkstra算法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
int n;
const int maxn = 200 + 10;
const int inf = 0x3f3f3f3f;
int x[maxn], y[maxn], vis[maxn];
double dist[maxn][maxn], d[maxn];
double solve()
{
memset(vis, 0, sizeof(vis));
for(int i = 1;i <= n; i++)
d[i] = dist[i][1];
for(int i = 1; i <= n; i++)
{
int x;
double minn = inf;
for(int j = 1; j <= n; j++)
{
if(!vis[j] && d[j] < minn)
{
x = j;
minn = d[j];
}
}
vis[x] = 1;
for(int j = 1; j <= n; j++)
d[j] = min(d[j], max(d[x], dist[x][j])); //dis[j]为从一号石头到第j号石头所有通路中最长边中的最小边
}
return d[2];
} int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int cnt = 0;
while(scanf("%d", &n) && n)
{
for(int i = 1; i <= n; i++)
scanf("%d %d", &x[i], &y[i]);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == j)
dist[i][j] = 0;
else
dist[i][j] = sqrt(double(x[i] - x[j])*(x[i] - x[j]) + double(y[i] - y[j])*(y[i] - y[j]));
}
}
printf("Scenario #%d\n", ++cnt);
printf("Frog Distance = ");
printf("%.3lf\n\n",solve());
}
}

利用floyd算法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 0x3f3f3f3f;
const int maxn = 200 + 10; int n;
double x[maxn], y[maxn];
double dist[maxn][maxn];
void Floyd()
{
for(int k = 1; k <= n; k++)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
dist[i][j] = min(dist[i][j], max(dist[i][k], dist[k][j]));
}
}
} int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int cnt = 0;
while(cin >> n && n)
{
for(int i = 1; i <= n; i++)
cin >> x[i] >> y[i];
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == j)
dist[i][j] = 0;
else
dist[i][j] = sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]));
}
}
Floyd();
cout << "Scenario #" << ++cnt << endl;
cout << "Frog Distance = " ;
cout << fixed << setprecision(3) << min(dist[1][2], dist[2][1]) << endl << endl; } }

类似题目

Frogger POJ - 2253(求两个石头之间”所有通路中最长边中“的最小边)的更多相关文章

  1. js 求两个日期之间相差天数

    //求两个日期之间的相差天数 function daysBetween(DateOne, DateTwo) { var OneMonth = DateOne.substring(5, DateOne. ...

  2. PHP 求两个日期之间相差的天数、月数

    <?php /** * 求两个日期之间相差的天数 * (针对1970年1月1日之后,求之前可以采用泰勒公式) * @param string $day1 * @param string $day ...

  3. CCF(地铁修建):向前星+dijikstra+求a到b所有路径中最长边中的最小值

    地铁修建 201703-4 这题就是最短路的一种变形,不是求两点之间的最短路,而是求所有路径中的最长边的最小值. 这里还是使用d数组,但是定义不同了,这里的d[i]就是表示从起点到i的路径中最长边中的 ...

  4. JavaScript求两个数字之间所有数字的和

    这是在fcc上的中级算法中的第一题,拉出来的原因并不是因为有什么好说的,而是我刚看时以为是求两个数字的和, 很显然错了.我感觉自己的文字理解能力被严重鄙视了- -.故拉出来折腾折腾. 要求: 给你一个 ...

  5. AJPFX:求两个城市之间的距离

    键盘录入多个城市: 城市1,城市2,城市3  以 ### 结束输出然后再键盘录入各个城市之间的距离:  格式如下:0,12,4512,0,2245,22,0### 然后按照输入的两个城市,求得两个城市 ...

  6. kuangbin专题专题四 Frogger POJ - 2253

    题目链接:https://vjudge.net/problem/POJ-2253 思路: 从一号到二号石头的所有路线中,每条路线中都个子选出该路线中两点通路的最长距离,并在这些选出的最长距离选出最短路 ...

  7. floyd类型题UVa-10099-The Tourist Guide +Frogger POJ - 2253

    The Tourist Guide Mr. G. works as a tourist guide. His current assignment is to take some tourists f ...

  8. Frogger - poj 2253 (Dijkstra)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28802   Accepted: 9353 Description Fr ...

  9. 设计一个Mypoint类,求两个点之间的距离

    package Test; public class test6 { public static void main(String[] args) { // TODO Auto-generated m ...

随机推荐

  1. WOE:信用评分卡模型中的变量离散化方法(生存分析)

    WOE:信用评分卡模型中的变量离散化方法 2016-03-21 生存分析 在做回归模型时,因临床需要常常需要对连续性的变量离散化,诸如年龄,分为老.中.青三组,一般的做法是ROC或者X-tile等等. ...

  2. vue v-for循环中key属性的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. P1039 大规模间谍入侵

    题目描述 爱丽丝魔法王国成立10周年,于是决定矩形国庆大阅兵. 在国庆大阅兵期间,为了防止暗黑王国的间谍乔装成平民混入,需要对每一个进城的人做检测. 因为暗黑王国的人长得和爱丽丝魔法王国的人长得很像, ...

  4. linux scull 中的缺陷

    让我们快速看一段 scull 内存管理代码. 在写逻辑的深处, scull 必须决定它请求的内 存是否已经分配. 处理这个任务的代码是: if (!dptr->data[s_pos]) { dp ...

  5. Array数组的常用方法

    1.concat()链接数组 var a = [1,2,3]; console.log(a.concat(4,5));//[1,2,3,4,5] 2.join()分隔符链接数组变字符串,如果省略该参数 ...

  6. H3C备份/恢复下次启动配置文件

  7. HBase的安装及使用

    一.摘要以前搜书吧的数据量比较小,使用数据库+静态文件存储的方式就可以搞定,主要有2个系统组成:网站前端+后台服务.事先把图书详情等一些固定内容生成html静态文件和前端的其他静态文件打包部署,动态变 ...

  8. git之github下载篇(ssh需要配置密钥)

    1.使用git命令行下载 在想要下载的文件夹打开命令行 git clone ssh 成功如图所示  2.使用小乌龟图形界面克隆 在文件夹右键鼠标 如果复制有链接,会自动填入.点击确定 成功后如图

  9. 想突破学习瓶颈,为什么要认真的学一下Dubbo?

    今天有学生在问,在学习dubbo的时候遇到瓶颈了怎么办,一些东西就感觉就在那里,但是,就是碰不到,摸不着,陷入了迷茫,今天在这里,就跟大家讲一下怎么突破这个瓶颈 先自我介绍一下哈,我是鲁班学院的周瑜老 ...

  10. mysql中information_schema.schemata字段说明

    1. 获取所有数据库信息(SCHEMATA) show databases; 查看用户下所有数据库信息:SCHEMATA表:提供了关于数据库中的库的信息.详细表述了某个库的名称,默认编码,排序规则.各 ...