ACM: 图论题 poj 2253 (poj上的dou…

来源:互联网 发布:淘宝网店加盟哪家好 编辑:程序博客网 时间:2024/05/19 00:35

                                                                            Frogger

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. Heplans to visit her, but since the water is dirty and full oftourists' sunscreen, he wants to avoid swimming and instead reachher by jumping.
Unfortunately Fiona's stone is out of his jump range. ThereforeFreddy considers to use other stones as intermediate stops andreach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviouslymust be at least as long as the longest jump occuring in thesequence.
The frog distance (humans also call it minimax distance) betweentwo stones therefore is defined as the minimum necessary jump rangeover all possible paths between the two stones.

You are given the coordinates of Freddy's stone, Fiona's stone andall other stones in the lake. Your job is to compute the frogdistance between Freddy's and Fiona's stone.

Input

The input will contain one or more test cases. The first line ofeach test case will contain the number of stones n(2<=n<=200). The next n lines eachcontain 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 othern-2 stones are unoccupied. There's a blank line following each testcase. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying "Scenario #x" and a linesaying "Frog Distance = y" where x is replaced by the test casenumber (they are numbered from 1) and y is replaced by theappropriate real number, printed to three decimals. Put a blankline 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

题意:  Freddy青蛙想跳到Fiona青蛙所在的石头.通过其他石头跳到目的地.
         找出每一步跳跃中最大的距离.但是在所有跳跃的paths总和是最小的.

解题思路:
          1.x,y轴上的点,要进行建图.求每两个点之间的距离.
          2.既然要最小中求最大的(题目要求),采取floyd算法进行迭代.(第一个思路AC 47MS)
          3.需要改动的是,记录的不再是路径的最短值.
        当 edges[i][k] <edges[i][j] && edges[k][j]< edges[i][j] 条件满足.
        记录下 max(edges[i][k] ,edges[k][j]) 中大的那个值. 最后得出edges[0][1]就是求解的结果.
         (因为输入时候,第0个石头到第1个石头分别是Freddy青蛙和Fiona青蛙所在的石头)

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

#define MAX 350

int info[MAX][2];
int edges[MAX][MAX];
int n;

int dist(int a1,int b1,int a2,int b2)
{
    return(a1-a2)*(a1-a2) + (b1-b2)*(b1-b2);
}

int max(int a,int b)
{
    returna>b ? a : b;
}

void read_graph()
{
    int i ,j;
    for(i = 0; i< n; ++i)
    {
      scanf("%d%d",&info[i][0],&info[i][1]);
    }
    
    for(i = 0; i< n; ++i)
    {
      for(j = 0; j < n; ++j)
      {
         edges[i][j] =dist(info[i][0],info[i][1],info[j][0],info[j][1]);
      }
    }
}


void floyd()
{
    for(int k =0; k <  n; ++k)
    {
      for(int i = 0; i < n; ++i)
      {
         for(int j = 0; j < n; ++j)
         {
            if(edges[i][k] < edges[i][j]&& edges[k][j] <edges[i][j])
            {
               edges[i][j] = max(edges[i][k],edges[k][j]);
            }
         }
      }
    }
}


int main()
{
//   freopen("input.txt","r",stdin);
    int num =1;
   while(scanf("%d",&n) != EOF&& n != 0)
    {
      read_graph();
      floyd();
      printf("Scenario #%d\n",num++);
      printf("Frog Distance =%.3lf\n",sqrt(edges[0][1]) + 0.00000001);
      printf("\n");
    }
    
    return0;
}

0 0
原创粉丝点击