poj 2253 Frogger (最短路径)

Frogger
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22557   Accepted: 7339

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

Source

dij:
 1 //348K    0MS    C++    1240B    2013-11-23 09:00:29
 2 /*
 3 
 4     题意:
 5         给出n个石头,互相连通,青蛙要从第一个石头跳到第二个,求所有通路中最小的
 6     各通路的最大跨步.
 7     
 8     最短路径:
 9         dij小变形,有点巧妙..看代码慢慢体会 
10 
11 */    
12 #include<stdio.h>
13 #include<string.h>
14 #include<math.h>
15 struct node{
16     int x,y;
17 }p[205];
18 int g[205][205];
19 int d[205];
20 bool vis[205];
21 int n;
22 int dis(node a,node b)
23 {
24     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
25 }
26 int max(int a,int b)
27 {
28     return a>b?a:b;
29 }
30 int min(int a,int b)
31 {
32     return a<b?a:b;
33 }
34 double dij()
35 {
36     memset(vis,false,sizeof(vis));
37     for(int i=0;i<n;i++){ 
38         d[i]=g[0][i];
39     }
40     vis[0]=true;
41     while(!vis[1]){ //遍历过第二个石头后就可跳出 
42         int temp=0x7ffffff;
43         int v=1;        
44         for(int j=0;j<n;j++)
45             if(!vis[j] && temp>d[j]){
46                 temp=d[j];
47                 v=j;
48             }
49         vis[v]=true;
50         for(int j=0;j<n;j++)
51             if(!vis[j])
52                 d[j]=min(max(d[v],g[v][j]),d[j]); //最小的最大步长 
53     }   
54     return sqrt(1.0*d[1]);
55 }
56 int main(void)
57 {
58     int k=1;
59     while(scanf("%d",&n),n)
60     {
61         for(int i=0;i<n;i++)
62             scanf("%d%d",&p[i].x,&p[i].y);
63         for(int i=0;i<n;i++)
64             for(int j=i+1;j<n;j++)
65                 g[i][j]=g[j][i]=dis(p[i],p[j]);
66         double ans=dij();
67         printf("Scenario #%d\n",k++);
68         printf("Frog Distance = %.3lf\n\n",ans);
69     }
70     return 0;
71 }
View Code

 

floyd:

 1 //348K    47MS    C++    943B    2013-11-23 09:19:27
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<math.h>
 5 struct node{
 6     int x,y;
 7 }p[205];
 8 int g[205][205];
 9 int n;
10 int dis(node a,node b)
11 {
12     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
13 }
14 int Max(int a,int b)
15 {
16     return a>b?a:b;
17 }
18 double floyd()
19 {
20     for(int k=0;k<n;k++)
21         for(int i=0;i<n-1;i++)
22             for(int j=i+1;j<n;j++)
23                 if(g[i][k]<g[i][j] && g[k][j]<g[i][j])
24                     g[i][j]=g[j][i]=Max(g[i][k],g[k][j]);
25     return sqrt(1.0*g[0][1]);
26 }
27 int main(void)
28 {
29     int k=1;
30     while(scanf("%d",&n),n)
31     {
32         for(int i=0;i<n;i++)
33             scanf("%d%d",&p[i].x,&p[i].y);
34         for(int i=0;i<n-1;i++)
35             for(int j=i+1;j<n;j++)
36                 g[i][j]=g[j][i]=dis(p[i],p[j]);
37         double ans=floyd();
38         printf("Scenario #%d\n",k++);
39         printf("Frog Distance = %.3lf\n\n",ans);
40     }
41     return 0;
42 }
View Code

 

注意小细节:使用G++交的话要把printf中的 lf 改成 f ,否则会报错!

 

posted @ 2013-11-23 09:21  heaventouch  阅读(242)  评论(0编辑  收藏  举报