您的位置:首页 > 其它

POJ 2253 Frogger(dijkstra变形)

2016-04-24 17:14 393 查看
题目描述:

Frogger

Time Limit: 1000MS Memory Limit: 65536K

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

Scenario #2

Frog Distance = 1.414

题目大意:

就是一直青蛙在水塘里找另外一直青蛙,给出了水塘里可以踩的石子的坐标,问这只青蛙每次跳跃的最小距离是多少

题目分析:

我的方法也是用最短路中dijkstra的思路来做的

数组d[i]:表示从起点调到第i号石子的最小的跳跃距离

优先队列que(小根堆)中存储二元组:{最小的跳跃距离,节点编号},因为每次都是优先用最短跳跃距离去更新其他节点,所以只要弹出了节点n,那么这个就是结果,因为当前优先队列中的其他的节点的最短跳跃距离大于等于当前的n,如果用其他节点再去更新其他节点的话,结果只会比现在弹出的n的最短跳跃距离更大,所以当前弹出的一定是最小的结果。

if(d[i]>max(d[no],dis)){
d[i]=max(d[no],dis);
que.push(P(d[i],i));
}


什么时候压栈?

因为池塘里的每一块石子都可以由另一块石子跳过来,所以每从que中弹出一个节点,如果到当前节点的最小跳跃距离小于d[i]的话,就更新节点,并压栈。

初始化:

从自己跳到自己的位置的最小跳跃距离当然是0啦,至于其他点,从实际意义考虑,最开始都跳不到,所以应该是INF,如果不明白的话,从上面代码中的max也能明白应该初始化为INF吧

代码:

#include "stdio.h"
#include "string.h"
#include "math.h"
#include "algorithm"
#include "vector"
#include "queue"
#include "utility"
#define INF 1e8
using namespace std;
typedef pair<double,int> P;
int n;
struct s
{
int x,y;
};
double d[200+5];
s stone[200+5];

int main()
{
int ca=1,i;
double res;
while(scanf("%d",&n),n)
{
for (i = 0; i < n; i++)
{
scanf("%d%d",&stone[i].x,&stone[i].y);
}
priority_queue<P,vector<P>,greater<P> > que;
fill(d,d+n,INF);
d[0]=0;
que.push(P(0,0));
while(!que.empty())
{
P p=que.top();que.pop();
int no=p.second;
if(no==1){
res=p.first;
break;
}
for(i=1;i<n;i++)
{
double dis=sqrt((stone[i].x-stone[no].x)*(stone[i].x-stone[no].x)+(stone[i].y-stone[no].y)*(stone[i].y-stone[no].y));
if(d[i]>max(d[no],dis)){ d[i]=max(d[no],dis); que.push(P(d[i],i)); }
}
}
printf("Scenario #%d\n",ca++);
printf("Frog Distance = %.3f\n\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj dijkstra