Frogger - poj 2253 (Dijkstra)

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

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 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这题可以用Dijkstra,将松弛条件改一下就可以了,改成
          if(dis[j]>max(dis[stone],map[stone][j])&&(vis[j]==0)){
              dis[j]=max(dis[stone],map[stone][j]);
          }
这样的结果就是求得能到达这点的路径上的最长边的最小值,求输出时要注意格式
 1 #include <iostream>
 2 #include<math.h>
 3 #include<limits.h>
 4 #include<algorithm>
 5 #include<iomanip>
 6 using namespace std;
 7 int num;
 8 int vis[200],stone[200][2];
 9 int map[200][200],dis[200];
10 int Dijkstra(){
11     for(int i=0;i<num;i++){
12         dis[i]=INT_MAX;
13         vis[i]=0;
14     }
15     dis[0]=0;
16     for(int i=0;i<num;i++){
17         int min=INT_MAX;
18         int stone;
19         for(int j=0;j<num;j++){
20             if((vis[j]==0)&&min>dis[j]){
21                 stone=j;
22                 min=dis[j];
23             }
24         }
25         vis[stone]=1;
26         if(min==INT_MAX)
27             break;
28         for(int j=0;j<num;j++){
29             if(dis[j]>max(dis[stone],map[stone][j])&&(vis[j]==0)){
30                 dis[j]=max(dis[stone],map[stone][j]);
31             }
32         }
33     }
34     return dis[1];
35 }
36
37 int main() {
38
39     cin>>num;
40     int count=1;
41     while(num){
42         for(int i=0;i<num;i++){
43             cin>>stone[i][0]>>stone[i][1];
44         }
45         for(int i=0;i<num;i++){
46             for(int j=0;j<num;j++){
47                 map[i][j]=pow((stone[i][0]-stone[j][0]),2)+pow((stone[i][1]-stone[j][1]),2);
48             }
49         }
50         float  fdis=sqrt(Dijkstra());
51         cout<<fixed;
52         cout<<"Scenario #"<<count<<endl<<"Frog Distance = "<<setprecision(3)<<fdis<<endl<<endl;
53
54         count++;
55         cin>>num;
56     }
57
58     return 0;
59 }
时间: 2024-10-08 01:30:06

Frogger - poj 2253 (Dijkstra)的相关文章

Poj(2253),Dijkstra松弛条件的变形

题目链接:http://poj.org/problem?id=2253 题意: 给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离. 现在要求求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离. 思路: j从1,2,两条路中选取较小者,而1这条路,是s—>k—>j的最大步伐. #include <stdio.h&g

POJ - 2253(dijkstra和Floyd变形)

题目链接:http://poj.org/problem?id=2253 题目意思:有n个石头的坐标,一只青蛙想从第一个坐标的石头跳到第二的石头上,问最短路径时要跳最长的路长为多少? 思路:就是将dijkstra,floyd中的d[],map[]存储的内容改为最短路中最长边. 有点无语的是,交到G++去了用printf("%.3lf"),精度有点问题wa了很多很多发.... 花了我两天,还以为算法错了.结果交c++就过了.真的心态爆炸.... 不过发现了一个保存精度的函数setpreci

Frogger POJ 2253

http://poj.org/problem?id=2253 题意:有一只青蛙A想要跳到另一只青蛙B处, 由于青蛙A的跳跃幅度没有那么大, 所以它需要借助它周边的一些石头从而到达青蛙B处.问你在众多可供选择的道路中,哪条道路中跳跃的最大值要比其他道路中的最大值小.(也就是求它的跳跃幅度至少要为多少) #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #inclu

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 from one city to another. Some two-way roads connect the cities. For each pair of neighboring cities there is a bus service that runs only between thos

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

题意 ? 题目主要说的是,有两只青蛙,在两个石头上,他们之间也有一些石头,一只青蛙要想到达另一只青蛙所在地方,必须跳在石头上.题目中给出了两只青蛙的初始位置,以及剩余石头的位置,问一只青蛙到达另一只青蛙所在地的所有路径中的"the frog distance"中的最小值. ? 解释一下"the frog distance": 题目中给出了一段解释"The frog distance (humans also call it minimax distance

B - Frogger POJ - 2253

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 rea

poj 2253 Frogger (dijkstra最短路)

题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25773   Accepted: 8374 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on an

POJ 2253 Frogger

题意:一只青蛙找到另外一只青蛙,不过可以通过其它的石头跳到目标青蛙的位置去,其中,输入数据的时候第一组数据是第一只青蛙的位置,第二组是目标青蛙的位置,其它的为石头的位置 思路:dijkstra算法的一种小小的变形,做法还是一样的 Tips:POJ上的双精度浮点型输出竟然是%f输出害的我一直错,或者是编译错误,恼啊! AC代码: #include<cstdio> #include<cmath> #include<algorithm> using namespace std

[2016-04-02][POJ][2253][Frogger]

时间:2016-04-02 17:55:33 星期六 题目编号:[2016-04-02][POJ][2253][Frogger] 题目大意:给定n个点的坐标,问从起点到终点的所有路径中,最大边的最小值是多少,即每一步至少是多少才能走到终点 分析: 方法1: 枚举出完全图,然后从起点跑一次Dijkstra算法,不过选点不再是选择起点到终点路径的点,而是起点到终点的路径中,边最大边最小的点,即d数组保存起点到当前点的路径中最大边的最小值, 最大边的最小值:u->v d[v] = min(d[i],m