poj 2253 Dijkstra
来源:互联网 发布:倒带和彩虹歌词知乎 编辑:程序博客网 时间:2024/05/21 11:22
Frogger
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 36854 Accepted: 11847
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.
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
20 03 4317 419 418 50
Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414
题意:有两只青蛙,分别在两个石头上,青蛙A想要到青蛙B那儿去.
他可以直接跳到B的石头上,也可以跳到其他石头上,再从其他石头跳到B那儿.
求青蛙从A到B的所有路径中最小的Frog Distance
例如,如果从A到B某条路径跳远的距离是2,5,6,4,则Frog Distance就是6
要求求出所有通路的最大距离Frog Distance,并把这些最大距离作比较,把最小的一个最大距离作为青蛙的最小跳远距离
对于数据2青蛙有两种方案
方案1:1-2则经过距离为2.000故此时Frog Distance=2.000
方案2:1-3-2 则经过距离分别是1.414 1.414 故此时Frog Distance=1.414
故所求的最小的Frog Distance=1.414
题解:使用dijkstra最短路径算法,在计算这个最短路径的时候计算上叙的最大跳远距离
注意:这里使用#define 定义一个常量比 const 定义的量要,赋值时间短一些
#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>using namespace std;#define INF 0x3f3f3f3fdouble path[300];int flag[300];double map[300][300];int n;struct coordinate{ int x,y;}a[300];double calc(int i,int j){ double x=a[i].x-a[j].x; double y=a[i].y-a[j].y; return sqrt(x*x+y*y);}int find_it(){ int p=-1; double min=INF; for(int i=1;i<=n;i++) if(!flag[i]&&path[i]<min) min=path[p=i]; return p;}double Dijkstra(){ for(int i=1;i<=n;i++){ flag[i]=0; path[i]=INF; } path[1]=0; int p=1; while(p!=-1) { flag[p]=1; for(int i=1;i<=n;i++){ double maxn=path[p]>map[p][i]?path[p]:map[p][i]; path[i]=path[i]<maxn?path[i]:maxn; } p=find_it(); } return path[2];}int main(){ int cases=1; //freopen("in.txt","r",stdin); while(scanf("%d",&n),n) { for(int i=1;i<=n;i++){ scanf("%d%d",&a[i].x,&a[i].y); for(int j=1;j<i;j++) map[j][i]=map[i][j]=calc(i,j); map[i][i]=0; } printf("Scenario #%d\nFrog Distance = %0.3lf\n\n",cases++,Dijkstra()); } return 0;}
0 0
- poj 2253 - Frogger --- dijkstra
- poj 2253 : Frogger (dijkstra)
- poj 2253 dijkstra
- POJ 2253 Dijkstra
- poj 2253 Frogger [dijkstra]
- POJ 2253 Dijkstra变形
- poj 2253 Dijkstra
- poj 2253 Frogger 【dijkstra】
- POJ. 2253 Frogger (Dijkstra )
- poj 2253 dijkstra法 dijkstra不能求负权!
- poj 2253 Frogger(dijkstra)
- POJ - 2253 - Frogger (Dijkstra)
- POJ 2253 Frogger(dijkstra)
- POJ 2253 Frogger (dijkstra)
- POJ - 2253 Frogger(Dijkstra)
- POJ-2253 Frogger(Dijkstra)(Floyd)
- 【POJ 2253】Frogger(Dijkstra)
- POJ 2253 Frogger(dijkstra变形)
- 来电归属地显示
- 强引用、弱引用、软引用、虚引用
- java中递归算法和汉诺塔
- Zephyr OS 基础篇:系统简介
- 线程池命名问题
- poj 2253 Dijkstra
- android开发工具类(草鸡好用)
- HDU 5311 Hidden String (DFS)
- 字符数组,字符指针,sizeof,strlen总结
- 集合框架
- servlet中request等中文乱码问题
- 当前不会命中断点还未为文档加载任何符号 解决方法
- 园圆形图片 ImageLoder 实现
- Spark开发指南