【每日一题(38)】Frogger POJ-2253

Frogger POJ-2253

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


扫描二维码关注公众号,回复: 3465148 查看本文章

Scenario #2
Frog Distance = 1.414

题解

题意是,第一个石子是出发地,第二个石子是目的地,求两者间所有路径的最大值的最小值
例如有三条路
1. 3→4→2→6→4 最大值是 6
2. 5→3→7→4 最大值是 7
3. 4→1→5→3→2 最大值是 5
于是,各个路径的最大值的最小值就是 5

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3fffffff
#define maxn 205
double mp[maxn][maxn],path[maxn];//mp存储两点间距离,path存从起点到i点的最大距离的最小值
bool flag[maxn];//标记是否被访问过
int n,cnt = 1;
map<int,pair<int,int> > m;//map存储点的x和y
struct node{
  int num;double dis;//num存储石头编号,dis存从起点到num石头的最大距离的最小值
  node(int a,double b){//初始化函数
    num = a;dis = b;
  }
  bool friend operator < (node a, node b){//优先队列重载 <
    return a.dis > b.dis;
  }
};
double dis(int a,int b){//两点间距离函数
  return sqrt(pow((double)(m[a].first - m[b].first),2) + pow((double)(m[a].second - m[b].second),2));
}
void dijkstra(){
  priority_queue<node> q;//堆优化dijkstra
  node start(0,0);//把第一个起点加入队列
  path[0] = 0;
  q.push(start);
  while(!q.empty()){
    node p = q.top();q.pop();
    if(flag[p.num] == true) continue;
    flag[p.num] = true;
    for(int i = 0;i < n; i++){
      if(path[i] > max(mp[i][p.num],path[p.num]) && flag[i] == false){
        path[i] = max(mp[i][p.num],path[p.num]);//更新path数组
        node temp(i,path[i]);
        q.push(temp);
      }
    }
  }
}
int main()
{
  while(scanf("%d",&n) != EOF && n){
    for(int i = 0;i < maxn; i++){
      fill(mp[i],mp[i] + maxn, INF);//初始化
      mp[i][i] = 0;
    }
    fill(path,path + maxn, INF);//初始化
    fill(flag,flag + maxn, false);//初始化
    for(int i = 0;i < n; i++){//map对于石头编号i到x,y的映射
      scanf("%d %d",&m[i].first,&m[i].second);
      for(int j = 0;j < i; j++){
        mp[i][j] = mp[j][i] = dis(i,j);//计算两点间距离
      }
    }
    dijkstra();
    printf("Scenario #%d\n",cnt);
    printf("Frog Distance = %.3f\n\n",path[1]);
    cnt++;
  }

  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40861916/article/details/80087958