博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kuangbin带你飞]专题四 最短路练习 B( POJ 2253) Frogger(spfa)
阅读量:4557 次
发布时间:2019-06-08

本文共 3484 字,大约阅读时间需要 11 分钟。

B - Frogger(spfa)

题目链接:

题目:

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 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
20 03 4317 419 418 50
Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414 题意:有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标, 现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路, 问从A到B的所有通路上的最大边,比如有  有两条通路  1(4)5 (3)2 代表1到5之间的边为4,  5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4,   另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4,用三种方法都能解决 思路:最短路模板题,稍微变化一下,就是每条路径选取最大的,选取这几条路径中最小的,即把模板中d[i]>d[now]+lu[now][i]改成d[i]>max(d[now],lu[now][i])就行了, WA了很多遍由于初始化的原因,spfa算法代码如下:
//// Created by hanyu on 2019/7/14.//#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;#define MAX 0x3f3f3f3fconst int maxn=1005;double d[maxn];double lu[maxn][maxn];bool book[maxn];int n;void spfa(int a){ for(int i=2;i<=n;i++){ d[i]=MAX; book[i]=false; } //memset(book,false,sizeof(book)) //memset(d,MAX,sizeof(d)) //还是别用以上注释用的memset初始化,就因为这个WA了很多遍,找了两个小时的错误,不晓得为啥 queue
qu; d[a]=false; book[a]=true; qu.push(a); int now; while(!qu.empty()) { now=qu.front(); qu.pop(); book[now]=false; for(int i=1;i<=n;i++) { if(d[i]>max(d[now],lu[now][i])) { d[i]=max(d[now],lu[now][i]); if(!book[i]) { qu.push(i); book[i]=true; } } } }}int main(){ int k=0; int a[maxn],b[maxn]; while(~scanf("%d",&n)) { if (n == 0) break; for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i], &b[i]); } for (int i = 1; i <= n; i++){ for (int j = 1; j <= i; j++) { lu[i][j] = lu[j][i] = sqrt(double(a[i] - a[j]) * (a[i] - a[j]) +double(b[i] - b[j]) * (b[i] - b[j])); } } spfa(1); k++; printf("Scenario #%d\n",k); printf("Frog Distance = %.3lf\n\n",d[2]); } return 0;}

 

转载于:https://www.cnblogs.com/Vampire6/p/11202573.html

你可能感兴趣的文章
数据模型
查看>>
HDU-4288 Coder 线段树
查看>>
HDU-1878 欧拉回路 判定是否存在欧拉回路
查看>>
大道至简读后感
查看>>
jQuery文档处理
查看>>
[国嵌攻略][119][Linux中断处理程序设计]
查看>>
upstream实现内网网站在公网访问
查看>>
排序——归并排序
查看>>
JAVA实现对服务器的访问的两种方法
查看>>
NYOJ 496 [巡回赛-拓扑排序]
查看>>
redis五种数据类型的使用
查看>>
Form表单中的onClick,onSubmit和submit
查看>>
Python-SocketServer源码
查看>>
JavaScript-基本数据类型
查看>>
CentOS 7.3 实体机启动 U 盘制作
查看>>
mysql数据库
查看>>
dede调用文章里的图片
查看>>
windows 窗体基本控件
查看>>
unix date 命令获取某日期的前一天
查看>>
python中set、list、dict内部实现原理
查看>>