Frogger POJ

来源:互联网 发布:java函数名命名规则 编辑:程序博客网 时间:2024/05/16 16:58
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
20 03 4317 419 418 50
Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414
dij算法的变形题目,让你求每一条联通路上的最大值的最小值.换句话说,你至少一步走多少才能到达目的地呢
dij算法上确定这一点为以最小点是通过排序,找到dist数组内的最小值来确定的,现在要求的问题仍然是最小,所以这个条件不用变化,要变化的是松弛条件.
最短路的松弛条件是dist[u]>dist[v]+w,那么这个应该改为通路距离上的最小值,就是从一个点出发,这个点的值与散出的边求一个最大值,如果大于最大值就操作,要不就不操作
上代码
#include<iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>using namespace std;#define maxn 205#define inf 999999.9double ma[maxn][maxn];double dist[maxn];int vis[maxn];double xy[maxn][2];int n;double far(double x1,double y1,double x2,double y2){    double f=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);    //printf("%.3lf\n",sqrt(f));    return sqrt(f);}double dijkstra(int u){    for(int i=1; i<=n; i++)    {        dist[i]=ma[u][i];        //printf("%.3lf\n",dist[i]);        vis[i]=0;    }    // printf("\n");    vis[u]=1;    double mi;    for(int i=1; i<=n-1; i++)    {        mi=inf;        int tm=0;        for(int j=1; j<=n; j++)        {            if(!vis[j]&&dist[j]<mi)            {                mi=dist[j];                tm=j;            }        }        vis[tm]=1;        for(int j=1; j<=n; j++)        {            if(!vis[j])            {                double maxx=max(dist[tm],ma[tm][j]); //dist记录的不是最短路径而是最短路径中的两个结点间的最短长度                if(dist[j]>maxx)                     dist[j]=maxx;            }        }    }    return dist[2];}int main(){    int ca=1;    while(scanf("%d",&n)!=EOF)    {        if(n==0)            return 0;        memset(ma,inf,sizeof(ma));        memset(dist,inf,sizeof(dist));        memset(vis,0,sizeof(vis));        for(int i=1; i<=n; i++)        {            scanf("%lf %lf",&xy[i][0],&xy[i][1]);        }        for(int i=1; i<=n; i++)        {            for(int j=1; j<i; j++)            {                if(i!=j)                {                    ma[i][j]=ma[j][i]=far(xy[i][0],xy[i][1],xy[j][0],xy[j][1]);                }            }            ma[i][i]=0;        }        double ans=dijkstra(1);        printf("Scenario #%d\n",ca);        ca++;        printf("Frog Distance = %.3f\n\n",ans);    }}


原创粉丝点击