POJ 2253-Frogger(最小生成树-给定终点)

柔情只为你懂 2022-07-11 12:25 222阅读 0赞

Frogger














Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40659   Accepted: 13051

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

Source

Ulm Local 1997

题目意思:

给出N个青蛙的坐标Xi,Yi,青蛙编号1~N。

青蛙1要从自己的位置跳到青蛙2的位置,求路程中所有路径中最长的一条的长度。

解题思路:

套了个prim的模板,改动的地方是:一旦目标点2被加入且更新后,最小树构建完毕,直接跳出循环。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <queue>
  6. #include <algorithm>
  7. #define MAXN 1010
  8. #define INF 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
  9. using namespace std;
  10. struct Node
  11. {
  12. int x,y;//坐标
  13. } po[MAXN];
  14. double cost[MAXN][MAXN],dis[MAXN],mincost[MAXN];//权值、最短路、最小生成树
  15. int n;
  16. bool used[MAXN];//标识是否使用过
  17. void prim()
  18. {
  19. fill(mincost,mincost+n+1,INF);
  20. fill(used,used+n+1,false);
  21. mincost[0]=0;
  22. double Max=-1;
  23. while(true)
  24. {
  25. int v=-1;
  26. for(int u=1; u<=n; ++u)
  27. {
  28. //从不属于已加入生成树的顶点中选取从已加入生成树的点到该顶点的权值最小的点
  29. if(!used[u]&&(v==-1||mincost[u]<mincost[v]))
  30. v=u;
  31. }
  32. if(v==-1) break;
  33. used[v]=true;
  34. if(Max<mincost[v]&&mincost[v]!=INF)
  35. Max=mincost[v];
  36. if(v==2) break;//2被加入,结束
  37. for(int u=1; u<=n; ++u)
  38. if(mincost[u]>cost[v][u])
  39. mincost[u]=cost[v][u];
  40. }
  41. cout.precision(3);
  42. cout.setf(ios::fixed);
  43. cout<<"Frog Distance = "<<Max<<endl<<endl;
  44. }
  45. int main()
  46. {
  47. #ifdef ONLINE_JUDGE
  48. #else
  49. freopen("F:/cb/read.txt","r",stdin);
  50. //freopen("F:/cb/out.txt","w",stdout);
  51. #endif
  52. ios::sync_with_stdio(false);
  53. cin.tie(0);
  54. int ca=0;
  55. while(cin>>n&&n)
  56. {
  57. for(int i=0; i<=n; ++i)
  58. for(int j=0; j<=n; ++j)
  59. cost[i][j]=INF;//手动初始化,不能fill了……
  60. for(int i=1; i<=n; i++)
  61. cin>>po[i].x>>po[i].y;
  62. for(int i=1; i<n; i++)
  63. for(int j=i+1; j<=n; j++)
  64. cost[i][j]=cost[j][i]=sqrt((po[i].x-po[j].x)*(po[i].x-po[j].x)+(po[i].y-po[j].y)*(po[i].y-po[j].y));
  65. cout<<"Scenario #"<<++ca<<endl;
  66. prim();
  67. }
  68. return 0;
  69. }
  70. /*
  71. 3
  72. 2 2 4 3 5
  73. 2 1 2 3 6
  74. 2 1 2 2 2
  75. 5
  76. 3 4 4 2 8 5 3
  77. 1 5 8
  78. 4 1 6 4 10 2 7 5 2
  79. 0
  80. 2 2 5 1 5
  81. 0
  82. */

测试数据:

  1. 2
  2. 0 0
  3. 3 4
  4. 3
  5. 17 4
  6. 19 4
  7. 18 5
  8. 8
  9. 1 1
  10. 4 0
  11. 1 2
  12. 2 2
  13. 3 2
  14. 4 2
  15. 3 0
  16. 5 1
  17. 3
  18. 9 10
  19. 10 10
  20. 100 10
  21. 6
  22. 5 5
  23. 100 100
  24. 4 4
  25. 3 3
  26. 2 2
  27. 1 1
  28. 5
  29. 1 2
  30. 2 1
  31. 3 2
  32. 4 1
  33. 5 2
  34. 3
  35. 999 999
  36. 1 1
  37. 3 3
  38. 0

结果:

  1. Scenario #1
  2. Frog Distance = 5.000
  3. Scenario #2
  4. Frog Distance = 1.414
  5. Scenario #3
  6. Frog Distance = 1.414
  7. Scenario #4
  8. Frog Distance = 1.000
  9. Scenario #5
  10. Frog Distance = 134.350
  11. Scenario #6
  12. Frog Distance = 1.414
  13. Scenario #7
  14. Frog Distance = 1408.557
  15. 感谢:yangyh,本测试用例为他所提供

发表评论

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

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

相关阅读

    相关 POJ2253 Frogger(Floyd)

    题目描述:青蛙A要找青蛙B,路径任选,求所有可能路径中跳的最远的一步,它们之中的最小距离值。 输入要求,第一行为石头数,二三行为起点和终点位置,n-2行为其他石头结点。第一