poj 2253

来源:互联网 发布:大o符号 算法 编辑:程序博客网 时间:2024/05/29 04:24

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
第1行坐标是起点,第2行坐标是终点,第3到第n行是可以跳到上面的石头的坐标,求从初始位置调到末位置,最少需要的跳远距离是多少,这个题可以用floyd算法的变形来做,在求最短路径的时候,floyd算法可以用动态规划的思想将每两个点通过第三个点进行松弛,然后这三个点就可以看做了一个整体,在这三个点中另外其他两个点中任意一个点作为桥梁时,除桥梁外的其他两个点又可以通过桥梁与其他的点进行松弛(这是我对弗洛伊德算法的理解)由于任何一个点都可以作为松弛的桥梁,所以最外层循环k需要循环n次,n为点的数目(这是题外话),ok,现在来说说floyd算法的变形,变形后,如果从I到j的距离大于从i到k而且大于从k到j的距离的话,那么我们就可以选择从i到k或者从k到j 二者中的最大跳跃距离来作为i到j的距离来进行松弛
这个题最后的输出真的坑,做了四五个小时,到现在依然不知道到底错哪了,可以看我代码下面标注的那里,崩了崩了= =  妈的
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<iomanip>using namespace std;struct p{    double x,y;}point[201];double path[201][201];int n;int main(){    int count=0;    while(scanf("%d",&n)&&n!=0)    {        for(int i=1;i<=n;++i)            scanf("%lf%lf",&point[i].x,&point[i].y);        for(int i=1;i<n;i++)            for(int j=i+1;j<=n;j++)            {            double x2=point[i].x-point[j].x;            double y2=point[i].y-point[j].y;            path[i][j]=path[j][i]=sqrt(x2*x2+y2*y2);            }    for(int k=1;k<=n;++k)        for(int i=1;i<=n;++i)        for(int j=1;j<=n;++j)        if(path[i][j]>max(path[i][k],path[k][j]))        path[i][j]=path[j][i]=max(path[i][k],path[k][j]);    //按标注释的来输出的话是错的,我也不知道为什么,求大神讲解    //printf("Scenario #%d\n",++count);    //printf("Frog Distance = %.3lf\n",path[1][2]);    cout<<"Scenario #"<<++count<<endl;    cout<<fixed<<setprecision(3)<<"Frog Distance = "<<path[1][2]<<endl;    cout<<endl;    }    return 0;}
下面这个也对   方法一样
#include <iostream>#include <string.h>#include <cstdio>#include <cmath>#include <iomanip>using namespace std;const int N=210;struct point{  double x,y;}pp[N];double map[N][N],dis[N][N];int numstone;double fun(double x){  return x*x;}double max(double a,double b){  return a>b?a:b;}int count=0;void floyd(){for(int i=1;i<=numstone;++i){  for(int j=1;j<=numstone;++j)  dis[i][j]=map[i][j];}for(int k=1;k<=numstone;++k){for(int i=1;i<=numstone;++i){for(int j=1;j<=numstone;++j){  if(dis[i][j]>max(dis[i][k],dis[k][j]))  dis[i][j]=max(dis[i][k],dis[k][j]);}}}cout<<"Scenario #"<<++count<<endl;    cout<<fixed<<setprecision(3)<<"Frog Distance = "<<dis[1][2]<<endl;    cout<<endl;}int main(){//freopen("1.txt","r",stdin);while(scanf("%d",&numstone),numstone){  for(int i=1;i<=numstone;++i)  scanf("%lf%lf",&pp[i].x,&pp[i].y);  for(int i=1;i<=numstone;++i){  for(int j=1;j<=numstone;++j){  map[i][j]=sqrt(fun(pp[j].x-pp[i].x)+fun(pp[j].y-pp[i].y));  }  }  floyd();}  return 0;}


原创粉丝点击