POJ - 2253 Frogger(迪杰斯特拉变形)

素颜马尾好姑娘i 2022-05-19 14:29 242阅读 0赞

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. 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

  1. 2
  2. 0 0
  3. 3 4
  4. 3
  5. 17 4
  6. 19 4
  7. 18 5
  8. 0

Sample Output

  1. Scenario #1
  2. Frog Distance = 5.000
  3. Scenario #2
  4. Frog Distance = 1.414

题意描述:

给出n 块石头及其坐标,求出1号石头到2号石头所经过路径中最大值最小的数(使青蛙不用一次跳太远的距离)

解题思路:

输入数据求出各个石头间的距离,存入二维数组中,将迪杰斯特拉算法中求路径的最短,判断dis[v]>dis[u]+map[u][v];更改为dis[v]>max(dis[u],map[u][p]);

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. # define inf 99999999
  5. double dis[210],map[210][210];//dis数组存储的为1到各个石头块路径中的最大值,更新使最大值最小
  6. int book[210],x[210],y[210];
  7. double max(double a,double b)
  8. {
  9. if(a>=b)
  10. return a;
  11. else
  12. return b;
  13. }
  14. int main()
  15. {
  16. int n,i,j,u,v,min,t;
  17. t=0;
  18. while(scanf("%d",&n))
  19. {
  20. if(n==0)
  21. break;
  22. t++;
  23. for(i=1;i<=n;i++)
  24. scanf("%d%d",&x[i],&y[i]);
  25. for(i=1;i<=n;i++)
  26. for(j=1;j<=n;j++)
  27. map[i][j]=0;
  28. //求出各个石头间的距离,存入地图
  29. for(i=1;i<n;i++)
  30. for(j=i+1;j<=n;j++)
  31. {
  32. map[i][j]=(double)sqrt((fabs(x[i]-x[j])*fabs(x[i]-x[j])+fabs(y[i]-y[j])*fabs(y[i]-y[j]))*1.0);
  33. map[j][i]=map[i][j];
  34. }
  35. for(i=1;i<=n;i++)
  36. dis[i]=map[1][i];
  37. memset(book,0,sizeof(book));
  38. book[1]=1;
  39. for(i=1;i<n;i++)
  40. {
  41. min=inf;
  42. for(j=1;j<=n;j++)
  43. {
  44. if(book[j]==0&&min>dis[j])
  45. {
  46. min=dis[j];
  47. u=j;
  48. }
  49. }
  50. book[u]=1;
  51. for(v=1;v<=n;v++)
  52. {
  53. if(dis[v]>max(dis[u],map[u][v]))//找到路径中的最大值都小,更改dis数组值
  54. dis[v]=max(dis[u],map[u][v]);
  55. }
  56. }
  57. printf("Scenario #%d\n",t);
  58. printf("Frog Distance = %.3f\n\n",dis[2]);
  59. }
  60. return 0;
  61. }

发表评论

表情:
评论列表 (有 0 条评论,242人围观)

还没有评论,来说两句吧...

相关阅读

    相关 算法

    迪杰斯特拉算法 迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法,解决

    相关 算法

    一:迪杰斯特拉算法 迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个结点到其他结点的最短路径。它的主要特点是以 起始点为中心向外层层扩展(广度优先搜索思