**题意描述
给出一系列的点,前两个点是青蛙的坐标,需要求的是第一个坐标到第二个坐标之间经历的最短距离。
解题思路:
还是Dijkstra算法做题,在数组进行存储的时候通过用数学方法求出知道的两个点之间的坐标求出他们之间的距离,然后存入对应的二维数组具体代码如下:
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
e[i][j]=e[j][i]=sqrt((x[i]-x[j])(x[i]-x[j])+(y[i]-y[j])(y[i]-y[j]));
另外再求最短距离的时候Dijkstra的核心算法改为
for(i=1; i<=n; i++)
{
min=inf;
for(j=1; j<=n; j++)
{
if(book[j]==0&&min>dis[j]){
u=j;
min=dis[j];
}
}
book[u]=1;
for(v=1; v<=n; v++)
{
if(dis[v]<inf)
{
***if(dis[v]>e[u][v]&&dis[v]>dis[u])
{
if(dis[u]>e[u][v])
dis[v]=dis[u];
else
dis[v]=e[u][v];
}***
}
}
英文题目
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
中文题目:
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
弗雷迪·青蛙坐在湖中的一块石头上。突然,他发现菲奥娜·青蛙坐在另一块石头上。他计划去看望她,但由于水很脏,而且有很多游客的防晒霜,所以他想避免游泳,而是跳到她跟前。
不幸的是菲奥娜的石头超出了他的跳跃范围。因此,弗雷迪考虑使用其他石头作为中间停止,并通过几次小跳跃的顺序到达她。
要执行给定的跳跃序列,青蛙的跳跃范围显然必须至少与序列中发生的最长跳跃相同。
因此,两块石头之间的青蛙距离(人类也称之为最小-最大距离)被定义为两块石头之间所有可能路径的最小必要跳跃范围。
你得到了弗雷迪的石头,菲奥娜的石头和湖中所有其他石头的坐标。你的工作是计算弗雷迪和菲奥娜石头之间的青蛙距离。
输入
输入将包含一个或多个测试用例。每个测试用例的第一行将包含石头数量n(2<=n<=200)。下n行各包含两个整数Xi,Yi(0<=Xi,Yi=1000),表示石头的坐标i。石头1是弗莱迪的石头,石头2是菲奥娜的石头,其他的N-2石头是空的。每个测试用例后面都有一个空白行。对于n,输入以零(0)的值终止。
输出
对于每个测试用例,打印一行“Scenario_x”和一行“Frog Distance=y”,其中x替换为测试用例编号(从1开始编号),y替换为适当的实数,打印为三位小数。在每个测试用例之后,甚至在最后一个测试用例之后,都放一个空白行。
AC代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
struct node{
double x;
double y;
}s[2000];
double dp[2000][2000];
double dis[20000];
int book[20000];
int inf=99999999;
int main()
{
int n,m,u,v,w,i,j,k,min,a=0;
double p,q;
while(scanf("%d", &n),n!=0)
{
memset(book,0,sizeof(book));
for(i=1; i<=n; i++)
scanf("%lf %lf", &s[i].x,&s[i].y);
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
p=s[i].x-s[j].x;
q=s[i].y-s[j].y;
dp[i][j]=dp[j][i]=sqrt(p*p+q*q);
}
}
for(i=1; i<=n; i++)
dis[i]=dp[1][i];
book[1]=1;
for(i=1; i<n; i++)
{
min=inf;
for(j=1; j<=n; j++)
{
if(book[j]==0&&dis[j]<min)
{
min=dis[j];
k=j;
}
}
book[k]=1;
for(v=1; v<=n; v++)
{
if(dp[k][v]<inf)
{
if(dis[v]>dis[k]&&dis[v]>dp[k][v])
dis[v]=(dis[k]>dp[u][v])?dis[k]:dp[u][v];
}
}
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n", ++a,dis[2]);
}
return 0;
}