POJ 2253 Frogger

题目链接:http://poj.org/problem?id=2253

 

Frogger
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 38366   Accepted: 12357

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

题目大意:input 第一行的n代表 在一个湖中有n个石头,接下来是每个石头的坐标,Freddy在第一个石头上,Fiona在第二个石头上,找出一条最短路径使的
Freddy跳到Fiona所在的石头上,输出这条路径中他需要跳的最远的一次的距离。

解题思路: 迪杰斯特拉算法的应用 将dis数组中的值变成最短路径中的最大权值
AC代码:
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <iomanip>
 7 using namespace std;
 8 #define inf 999999999
 9 double dis[220];
10 int visit[220];
11 double p[220][220];
12 int n;
13 void dijkstra()
14 {
15     int i,j,pos = 0,minn;
16     memset(visit,0,sizeof(visit));
17     visit[1] = 1;
18     dis[1] = 0;
19     for (i = 2; i <= n; i ++)
20         dis[i] = p[1][i];
21 
22     for (i = 1; i <= n; i ++)
23     {
24         minn = inf;
25         for (j = 1; j <= n; j ++)
26         {
27             if (!visit[j] && dis[j] < minn)
28             {
29                 minn = dis[j];
30                 pos = j;
31             }
32         }
33         visit[pos] = 1;
34         if (pos == 2)
35             break;
36         for (j = 1; j <= n; j ++)  //将dis数组中的值变成最短路径中的最大权值
37         {
38             if (!visit[j] && dis[j] > max(dis[pos],p[pos][j]))
39                 dis[j] = max(dis[pos],p[pos][j]);
40         }
41     }
42 }
43 int main ()
44 {
45     double x[220],y[220];
46     int i,j,f = 1;
47     while (~scanf("%d",&n))
48     {
49         if (n == 0)
50             break;
51         for (i = 1; i <= n; i ++)
52             for (j = 1; j <= n; j ++)
53                 p[i][j] = inf;
54 
55         for (i = 1; i <= n; i ++)
56             scanf("%lf%lf",x+i,y+i);
57 
58         for (i = 1;i < n; i ++)
59         {
60             for (j = i+1; j <= n; j ++)
61             {
62                 double xx = x[i]-x[j];
63                 double yy = y[i]-y[j];
64                 p[j][i] = p[i][j] = (double)sqrt(xx*xx+yy*yy);
65             }
66         }
67         dijkstra();
68         printf("Scenario #%d\n",f ++);
69         //printf("Frog Distance = %.3lf\n",dis[2]);  //不知道为什么这种输出一直WA 下面的AC
70         cout<<fixed<<setprecision(3)<<"Frog Distance = "<<dis[2]<<endl;  //setprecision需要加上头文件#include <iomanip>
71         cout<<endl;
72     }
73 }
View Code

 

 
posted @ 2016-09-11 19:31  gaoyanliang  阅读(297)  评论(0编辑  收藏  举报