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

POJ2253 Frogger

时间:2020-02-15 00:13:57      阅读:39      评论:0      收藏:0      [点我收藏+]

标签:lan   inter   another   min   line   seve   tun   notice   iostream   

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

大意就是求任意两个点之间所有路径中最大两点距离的最小值。可以看作是Floyd的变形,只需要把转移方程修改一下即可:d[i][j]=min(d[i][j],max(d[i][k],d[k][j]))其中d[i][j]表示i到j所有路程经过的最长边的最小值。
当然也可以用dijkstra来写,松弛操作改为:if(!vis[y] && d[y]>max(d[x],a[x][y]))
            d[y]=max(d[x],a[x][y]);
这其实也算是某种意义上的最短路,只不过定义不同。g++提交的话记得要把.3lf改成.3f才能过。
//Floyd 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
struct node
{
    double x;
    double y;
}nod[205];
double d[205][205];
double floyd()
{
    int i,j,k;
    for(k=1;k<=n;k++)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                d[i][j]=min(d[i][j],max(d[i][k],d[k][j]));
            }
        }
    }
    return d[1][2];
}
int main()
{
    int cnt=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        cnt++;
        int i,j;
        //memset(d,0x3f,sizeof(d)); double不能用memset 
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                d[i][j]=1e9; 
            } 
        }
        for(i=1;i<=n;i++)
        {
            double x,y;
            scanf("%lf%lf",&x,&y);
            nod[i].x=x;
            nod[i].y=y;
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                double len=sqrt((nod[i].x-nod[j].x)*(nod[i].x-nod[j].x)+(nod[i].y-nod[j].y)*(nod[i].y-nod[j].y));
                d[i][j]=d[j][i]=min(d[i][j],len);
                cout<<d[i][j]<<endl;
            } 
        }
        double out=floyd();
        printf("Scenario #%d\n",cnt);
        printf("Frog Distance = %.3lf\n\n",out);
    }
} 

//Dijkstra
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
double a[305][305];
double d[305];
bool vis[305];
int n;
struct node
{
    double x;
    double y;
}nod[205];
void dijkstra()
{

    d[1]=0;
    int i;
    for(i=1;i<n;i++)
    {
        int x=0;
        int j;
        for(j=1;j<=n;j++)
        {
            if(!vis[j]&&(x==0||d[j]<d[x]))x=j;
        }
        vis[x]=1;
        int y;
        for(y=1;y<=n;y++)
        {
            
            
        }
    }
}
int main()
{
    int cnt=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        cnt++;
        int i,j;
        //memset(d,0x3f,sizeof(d)); double不能用memset 
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                a[i][j]=1e9; 
            } 
            d[i]=1e9;
            vis[i]=0;
        }
        for(i=1;i<=n;i++)
        {
            double x,y;
            scanf("%lf%lf",&x,&y);
            nod[i].x=x;
            nod[i].y=y;
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                double len=sqrt((nod[i].x-nod[j].x)*(nod[i].x-nod[j].x)+(nod[i].y-nod[j].y)*(nod[i].y-nod[j].y));
                a[i][j]=a[j][i]=min(a[i][j],len);
            } 
        }
//        for(i=1;i<=n;i++)
//        {
//            for(j=1;j<=n;j++)
//            {
//                cout<<a[i][j]<<‘ ‘;
//            } 
//            cout<<endl;
//        }

        dijkstra();
        printf("Scenario #%d\nFrog Distance = %.3lf\n\n",cnt,d[2]);
    }
}

 


POJ2253 Frogger

标签:lan   inter   another   min   line   seve   tun   notice   iostream   

原文地址:https://www.cnblogs.com/lipoicyclic/p/12310323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!