Frogger

来源:互联网 发布:单片机驱动程序 编辑:程序博客网 时间:2024/06/05 02:52

Frogger

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 168   Accepted Submission(s) : 33
Problem 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
20 03 4317 419 418 50
 

Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414

求最短路径的最大值,错了这么多次是printf用错了

代码(prime):

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;const double inf=1e6;struct node{    double x,y;}p[300];double cnt,a[300][300],dis[300];bool ok[300];int main(){    int m,n,i,j,cas=0;    while(scanf("%d",&n)&&n)    {        for(i=1;i<=n;i++)            scanf("%lf %lf",&p[i].x,&p[i].y);        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)        {            a[i][j]=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));        }        memset(dis,127,sizeof(dis));        memset(ok,true,sizeof(ok));        cnt=0; dis[1]=0;        for(i=1;i<=n;i++)        {            m=0;            for(j=1;j<=n;j++)                if(ok[j]&&dis[m]>dis[j])            {                m=j;            }            ok[m]=false;            if(cnt<dis[m]&&dis[m]<inf)                cnt=dis[m];            if(m==2) break;            for(j=1;j<=n;j++)            {                if(ok[j]&&a[j][m]<dis[j])                    dis[j]=a[j][m];            }        }        printf("Scenario #%d\n",++cas);        printf("Frog Distance = %.3f\n",cnt);        printf("\n");    }    return 0;}

代码:

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;struct node{    int x,y;}p[205];double max(int & a,int & b){    return a>b?a:b;}double a[205][205];int main(){    int n,i,j,k,cas=0;    while(scanf("%d",&n)&&n)    {        memset(p,0,sizeof(p));        for(i=1;i<=n;i++)            scanf("%d%d",&p[i].x,&p[i].y);        memset(a,0x7f,sizeof(a));        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)        {            a[j][i]=a[i][j]=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));        }        for(k=1;k<=n;k++)            for(i=1;i<=n;i++)                for(j=1;j<=n;j++)                {                    if(k!=i&&i!=j&&k!=j)                    {                        if(a[i][j]>max(a[i][k],a[j][k]))                        a[i][j]=max(a[i][k],a[j][k]);                    }                }        printf("Scenario #%d\n",++cas);        printf("Frog Distance = %.3f\n",a[1][2]);        printf("\n");    }    return 0;}