poj 2253 (最大值里面求最小值 最短路的变形Dijkstra OR Floyd)

题目:

Frogger
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40328   Accepted: 12960

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坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的所有通路上的最大边,比如有  有两条通路  1(4)5 (3)2 代表1到5之间的边为4,  5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4,  另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4,用Dijkstra与Floyed方法都能解决

下面是Dijkstra

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<iomanip>
#include<queue>
using namespace std;
#define rep(i,a,b)   for(int i=a;i<=b;i++)
double e[300][300];
int n,t;
bool book[205];
double dis[205];//此处的dis[i]数组是表示在所有可能的路径当中从1到i顶点边的最小值,就比如1-(2)-3-(4)-8与1-(4)-4-(5)-8;dis[8]=4;
int MAX=0x3f3f3f3f;
struct node {
    double x;
    double y;
}num[205];

double Dijkstra()
{
    memset(book,0,sizeof(book));
    rep(i,1,n)
    dis[i]=e[1][i];
    //dis[1]=0;
    rep(i,1,n)
    {
        int MIN=MAX;
        int u;
        rep(j,1,n)
        {
            if(dis[j]<MIN&&!book[j])
            {
                MIN=dis[u=j];//其实这道题不用每次从最小的点开始也可以,去掉这个if也是可以AC的,但要两次for循环,感觉去掉之后就是Flory了
            }
        }
        book[u]=1;
        rep(k,1,n)
        {
            dis[k]=min(dis[k],max(dis[u],e[u][k]));
        }
    }
    return dis[2];
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T=1;
    while(cin>>n&&n)
    {
        memset(e,MAX,sizeof(e));
        rep(i,1,n)
        {
            cin>>num[i].x>>num[i].y;
        }
        rep(i,1,n)
        {
            rep(j,i+1,n)
            {
                e[i][j]=sqrt(pow(num[i].x-num[j].x,2)+pow(num[i].y-num[j].y,2));
                e[j][i]=e[i][j];
            }
            e[i][i]=0;
        }
        cout<<"Scenario #"<<T++<<endl;
        cout<<"Frog Distance = ";
        cout<<setiosflags(ios::fixed)<<setprecision(3)<<Dijkstra()<<endl;
        cout<<endl;
    }
    return 0;
}
下面是Flory的
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<iomanip>
#include<queue>
using namespace std;
#define rep(i,a,b)   for(int i=a;i<=b;i++)
double e[300][300];
int n,t;
bool book[205];
double dis[205];
int MAX=0x3f3f3f3f;
struct node {
    double x;
    double y;
}num[205];

double Floyd()
{

   rep(k,1,n)
   {
       rep(i,1,n)
       {
           rep(j,1,n)//这里一定是从1开始的
           e[i][j]=min(e[i][j],max(e[i][k],e[k][j]));
       }
   }
    return e[1][2];
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T=1;
    while(cin>>n&&n)
    {
        memset(e,MAX,sizeof(e));
        rep(i,1,n)
        {
            cin>>num[i].x>>num[i].y;
        }
        rep(i,1,n)
        {
            rep(j,i+1,n)
            {
                e[i][j]=sqrt(pow(num[i].x-num[j].x,2)+pow(num[i].y-num[j].y,2));
                e[j][i]=e[i][j];
            }
            e[i][i]=0;
        }
        cout<<"Scenario #"<<T++<<endl;
        cout<<"Frog Distance = ";
        cout<<setiosflags(ios::fixed)<<setprecision(3)<<Floyd()<<endl;
        cout<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/c___c18/article/details/80987653