POJ 2253:Frogger:dij的最短路思想变型
2014-07-14 20:06
393 查看
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
Sample Output
Source
Ulm Local 1997
这道题目乍一看跟最短路没有关系,但是它使用的是类似dij最短路操作的动态规划思想。算做dij最短路的一种变形。这个算法,自己想出来,成功了。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24809 | Accepted: 8056 |
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
Source
Ulm Local 1997
这道题目乍一看跟最短路没有关系,但是它使用的是类似dij最短路操作的动态规划思想。算做dij最短路的一种变形。这个算法,自己想出来,成功了。
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define maxn 10000000 int n; typedef struct node { double x,y; }node; node b[205]; double a[205][205],d[205]; int use[205]; void init() { int i,j; for(i=1;i<=n;i++) scanf("%lf%lf",&b[i].x,&b[i].y); memset(use,0,sizeof(use)); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { a[i][j]=a[j][i]=sqrt((b[i].x-b[j].x)*(b[i].x-b[j].x)+(b[i].y-b[j].y)*(b[i].y-b[j].y)); } d[i]=maxn; } } double maxx(double x,double y) { if(x>y) return x; return y; } void fdij() { int i,j; double tmin,tem; int tid; use[1]=1; for(i=1;i<=n;i++) d[i]=a[1][i]; while(1) { tmin=maxn; tid=-1; for(i=2;i<=n;i++) { if(!use[i]&&d[i]<tmin) { tmin=d[i]; tid=i; } } if(tid==2||tid==-1) break; use[tid]=1; for(i=2;i<=n;i++) { if(!use[i]) { tem=maxx(d[tid],a[tid][i]); d[i]=d[i]<=tem?d[i]:tem; } } } } void print(int c) { int i,j; printf("Scenario #%d\nFrog Distance = %.3f\n\n",c,d[2]); } int main() { int i=1,j; while(scanf("%d",&n)!=EOF&&n) { init(); fdij(); print(i++); } return 0; }
相关文章推荐
- POJ 2253 Frogger (单源最短路变型 求路径上最大边权值的最小值)
- POJ-2253 Frogger (最短路)
- 【Poj】-2253-Frogger(最短路,变化)
- 最短路(Floyd_Warshall) POJ 2253 Frogger
- POJ 2253 Frogger最短路(floyd)
- POJ 2253 Frogger 二分 + 最短路
- 文章标题 POJ 2253 : Frogger(最短路--Floyd)
- 【最短路中求至少需要的最短跳跃距离】poj 2253 Frogger
- POJ 2253 Frogger(最短路变形)
- POJ 题目2253 Frogger(最短路变形)
- POJ 2253Frogger(最短路floyd)
- POJ2253---Frogger(最短路变形)
- Poj_2253 Frogger(最短路-SPFA+Dijkstra)
- POJ 2253 Frogger 最短路
- POJ 2253 Frogger (最小生成树 or 最短路变形)
- POJ-2253 Frogger(利用dijkstra最短路求最大路径最小边)
- POJ 2253 Frogger (最短路,floyed)
- POJ 2253 Frogger (最短路)
- Floyd-Warshall算法(求解任意两点间的最短路) 详解 + 变形 之 poj 2253 Frogger
- POj 2253 Frogger(变相最短路)