码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2253 Frogger(最短路)

时间:2015-10-22 16:59:10      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

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

大致题意:给出两只青蛙的坐标A、B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的。显然从A到B存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。

现在要求求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离。

下面给出 floyd算法的代码
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <queue>
 6 #include <algorithm>
 7 using namespace std;
 8 const int maxn=210;
 9 struct node
10 {
11     double x,y;
12 }path[maxn];
13 double g[maxn][maxn]; //存放权值
14 int n,cnt=0;
15 void floyd()
16 {
17     int i,j,k;
18     for(k=1;k<=n;k++)
19     {
20         for(i=1;i<n;i++)
21         {
22             for(j=1;j<=n;j++)
23             {
24                 if(g[i][k]<g[i][j]&&g[k][j]<g[i][j])
25                 {
26                     if(g[i][k]<g[k][j])
27                         g[i][j]=g[j][i]=g[k][j];
28                     else
29                         g[i][j]=g[j][i]=g[i][k];
30                 }
31             }
32         }
33     }
34     printf("Scenario #%d\n",++cnt);
35     printf("Frog Distance = %.3lf\n\n",g[1][2]);
36     return ;
37 }
38 int main()
39 {
40     while(cin>>n,n)
41     {
42         for(int i=1;i<=n;i++)
43             cin>>path[i].x>>path[i].y;
44         for(int i=1;i<n;i++)
45         {
46             for(int j=i+1;j<=n;j++)
47             {
48                 double a=path[i].x-path[j].x;
49                 double b=path[i].y-path[j].y;
50                 g[i][j]=g[j][i]=sqrt(a*a+b*b); //双向连通图
51             }
52         }
53         floyd();
54     }
55     return 0;
56 }
View Code

 


下面给出Dijkstra算法的代码
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <queue>
 7 #include <algorithm>
 8 using namespace std;
 9 #define INF 1010
10 //Dijkstra算法
11 const int maxn=210;
12 struct node
13 {
14     double x,y;
15 } path[maxn];
16 double g[maxn][maxn];
17 double dis[maxn];
18 bool vis[maxn];
19 int cnt=0;
20 int n;
21 void Dijkstra()
22 {
23     memset(vis,false,sizeof(vis));
24     double Min;
25     int k;
26     for(int i=1; i<=n; i++)
27     {
28         dis[i]=g[1][i];
29         vis[i]=0;
30     }
31     vis[1]=1;
32     for(int i=1; i<n; i++)
33     {
34         Min=INF,k=-1;
35         for(int j=1; j<=n; j++)
36         {
37             if(!vis[j]&&dis[j]<Min)
38             {
39                 Min=dis[j];
40                 k=j;
41             }
42         }
43         vis[k]=1;
44         for(int j=1; j<=n; j++)
45         {
46             if(!vis[j]&&dis[j]>max(g[k][j],dis[k]))
47                 dis[j]=max(g[k][j],dis[k]);
48         }
49     }
50     printf("Scenario #%d\n",++cnt);
51     printf("Frog Distance = %.3lf\n\n",dis[2]);
52     return ;
53 }
54 int main()
55 {
56     double a,b;
57     while(scanf("%d",&n),n)
58     {
59         for(int i=1; i<=n; i++)
60             scanf("%lf%lf",&path[i].x,&path[i].y);
61         for(int i=1; i<n; i++) //无论怎样走,都会到达终点
62         {
63             for(int j=i+1; j<=n; j++)
64             {
65                 a=path[i].x-path[j].x;
66                 b=path[i].y-path[j].y;
67                 g[i][j]=g[j][i]=sqrt(a*a+b*b); //双向连通图
68             }
69         }
70         Dijkstra();
71     }
72     return 0;
73 }
View Code

 




poj 2253 Frogger(最短路)

标签:

原文地址:http://www.cnblogs.com/cxbky/p/4901221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!