poj 2253 Frogger dijkstra算法实现
2013-08-21 14:18
309 查看
点击打开链接
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
题目大意就是说一个青蛙想从一个石头跳到另一个石头上,这两个石头之间有很多其他的石头,所以青蛙有很多路径可以选择,题目要我们求出跳跃范围最少多远,青蛙才能找到一条跳过去的路,很明显,这个距离必须是这条路径上最长的。
使用Dijkstra算法,虽然这个算法是求最短路径的,而这个题目跟最短路径没有半毛钱关系,但是我们只需要把Dijkstra算法中松弛的条件改一下,就可以了,原来的dij算法中只有当前距离小于已经记录的距离的时候,会执行松弛操作,现在把这个距离改成从起点到当前点的最短跳跃距离,那么当这个最短跳跃距离变小的时候,就松弛一下
Frogger
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21653 | Accepted: 7042 |
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算法,虽然这个算法是求最短路径的,而这个题目跟最短路径没有半毛钱关系,但是我们只需要把Dijkstra算法中松弛的条件改一下,就可以了,原来的dij算法中只有当前距离小于已经记录的距离的时候,会执行松弛操作,现在把这个距离改成从起点到当前点的最短跳跃距离,那么当这个最短跳跃距离变小的时候,就松弛一下
#include<stdio.h> #include<math.h> #include<string.h> int n, i = 1; double map[202][2]; double dis[202][202]; double d[202]; void dij() { bool flag[202] = {0}; int j, k; int curr = 1; for(j = 1; j <= n; j++) { d[j] = 0x7fffffff; } d[1] = 0; for(j = 1; j <= n; j++) { int mark = -1; double mindis = 0x7fffffff; for(k = 1; k <= n; k++) { if(flag[k] == 0 && d[k] < mindis) { mindis = d[k]; mark = k; } } flag[mark] = 1; for(k = 1; k <= n; k++) { d[k] = (d[mark] > dis[mark][k] ? d[mark] : dis[mark][k]) > d[k] ? d[k] : (d[mark] > dis[mark][k] ? d[mark] : dis[mark][k]); } } } int main() { double x, y; while(scanf("%d", &n) != EOF) { if(n == 0) break; int j, k; for(j = 1; j <= n; j++) { scanf("%lf %lf", &x, &y); map[j][0] = x; map[j][1] = y; for(k = 1; k < j; k++) { double temp; temp = sqrt((x - map[k][0]) * (x - map[k][0]) + (y - map[k][1]) * (y - map[k][1])); dis[j][k] = temp; dis[k][j] = temp; } } dij(); printf("Scenario #%d\nFrog Distance = %.3lf\n\n", i, d[2]); i++; } return 0; }
相关文章推荐
- poj 2253 Frogger dijkstra算法实现
- POJ-2253 Frogger(最短路 Dijkstra算法)
- POJ 2253 Frogger 图论 Dijkstra算法
- POJ 2253 Frogger (dijkstra算法 + 预处理)
- POJ 2253 Frogger(最短路径Dijkstra算法和floyd算法)
- POJ 2253 Frogger
- poj 2253 Frogger
- POJ 2253 Frogger 二分 + 最短路
- B - Frogger POJ - 2253
- poj2253 Frogger
- POJ 2253Frogger(dijk最短路变形)
- POJ 2253 Frogger (floyd, 二分)
- 初级->图算法->最短路径 poj 2253 Frogger
- POJ - 2253 Frogger(并查集,二分)
- 文章标题 POJ 2253 : Frogger(最短路--Floyd)
- POJ 2253 Frogger
- poj 2253 Frogger (最短路变种,连通图的最长边)
- POJ_2253_Frogger_最短路
- POJ 2253 Frogger (单源最短路变型 求路径上最大边权值的最小值)
- poj 2253 Frogger【最短路】