1. 程式人生 > >POJ-2253-Frogger(最短路變形)

POJ-2253-Frogger(最短路變形)

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

思路:青蛙距離就是,存在一條路徑,這條路徑的每一步的最大值,是所有路徑裡最小的。這個最大值就是所謂青蛙路徑。我們吧Dijk演算法中的判斷兩點中間路徑,兩段短還是一段短,換成兩段的每段是不是都短於一段就ok。

坑點:這個題有大坑!關於編譯器的。

1.由於G++保留了早期優化,所以資料型別會有變化。用double輸入就要用%f輸出而不能用%lf。

具體如下

C++也能用%f交,所以輸出都用%f就好了

以上內容轉自(OJ提交題目中的語言選項裡G++與C++的區別):https://blog.csdn.net/bat67/article/details/61926650

2.G++中sqrt()可以開方int型別,而C++中sqrt()要轉化為(double)型別。所以安全起見都轉...

3.注意輸出格式...每個輸出之間有一行間距...

 


 

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define Inf 0x3f3f3f3f
 6 using namespace std;
 7 int n;
 8 double G[205][205],dis[205];
 9 int mark[205];
10 
11 struct node{
12     int x,y;
13 }p[205];
14 
15 double d(int x1,int y1,int x2,int y2){
16     return sqrt((double)(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
17 }
18 
19 void Getmap(){
20     for(int i=1;i<=n;i++){
21         scanf("%d%d",&p[i].x,&p[i].y);
22     } 
23     memset(G,Inf,sizeof(G));
24     for(int i=1;i<=n;i++){
25         G[i][i]=0;
26         for(int j=i+1;j<=n;j++){
27             G[i][j]=G[j][i]=d(p[i].x,p[i].y,p[j].x,p[j].y);
28         }        
29     }    
30 }
31 
32 void Dijk(){
33     int mini,p=1;
34     memset(mark,0,sizeof(mark));
35     for(int i=1;i<=n;i++)
36         dis[i]=G[1][i];    
37     for(int k=0;k<=n;k++){
38         mini=Inf;
39         for(int i=1;i<=n;i++){
40             if(!mark[i]&&dis[i]<mini){
41                 mini=dis[i];
42                 p=i;
43             }
44         }
45         mark[p]=1;
46         for(int i=1;i<=n;i++){
47             if(dis[i]>max(dis[p],G[p][i])){
48                 dis[i]=max(dis[p],G[p][i]);
49             }
50         }
51     }        
52 }
53 
54 int main(){
55     int cnt=0;
56     while(scanf("%d",&n)&&n){
57     Getmap();
58      Dijk();
59     printf("Scenario #%d\nFrog Distance = %.3f\n\n",++cnt,dis[2]);//這裡G++不能是%.3lf 
60     }
61     return 0;
62 }