POJ 2253 Frogger 最短路径 Floyed-Warshall算法

Frogger
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 31519 Accepted: 10163

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

Ulm Local 1997

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


题目大体意思是青蛙在第一块石头跳到第二块石头上可以经过其他石头,问所有路径的最大边的最小值
路径及两块石头的直线距离因为给出了笛卡尔坐标直接可求
然后用 Floyed-Warshall进行循环
for k  :1  to   n    do
   for i : 1   to   n    do
       for j  : 1  to  n   do
        当ik,kj的权值都小于i的时后则走i-->k-->j路线否则走i-->j路线
            当走i-->k-->j路线的时候选择max{ik,kj}//最大跳才能保持连通
这样就更新了邻接矩阵
8
0 0
2 4
0 2
1 0
2 0
2 1
2 2
2 3
这组数据跟新前后分别为
POJ 2253 Frogger 最短路径 Floyed-Warshall算法_第1张图片
然后输出path[1][2]的值就好啦
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 1005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
double xx[maxn],yy[maxn];
double path[maxn][maxn];
int main(){
    int n;
    int cnt=1;
    while(rd(n)&&n){
        int x,y;
        FOR(i,1,n)
                cin>>xx[i]>>yy[i];
        FOR(i,1,n)
            FOR(j,1,n)
                path[i][j]=sqrt((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]));
    /*FOR(i,1,n){
            FOR(j,1,n)
                printf("%.3f ",path[i][j]);
                putchar('\n');
            }*/
        FOR(k,1,n)
            FOR(i,1,n)
                FOR(j,1,n)
                    if(path[i][k]<path[i][j]&&path[k][j]<path[i][j])
                        if(path[i][k]<path[k][j])
                            path[i][j]=path[j][i]+path[k][j];
                        else
                            path[i][j]=path[j][i]=path[i][k];
          /*  printf("\n\n-----------------------------------\n\n");
            FOR(i,1,n){
                FOR(j,1,n)
                    printf("%.3f ",path[i][j]);
                    putchar('\n');
            }*/
            printf("Scenario #%d\nFrog Distance = %.3f\n\n",cnt++,path[1][2]);

    }
    return 0;
}
/*
2
0 0
3 4

3
17 4
19 4
18 5

0
*/


     

你可能感兴趣的:(POJ 2253 Frogger 最短路径 Floyed-Warshall算法)