首页 > 代码库 > Forgger
Forgger
2017-08-08
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
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
题目大意 :有两只青蛙,其中一只想要去拜访另一只青蛙。但它有点懒,不想一步跳的太远,它宁愿一步跳少点,多跳几次。问青蛙在这个过程中,跳的最小的一步是多少。
题目分析 :怎么说呢?一开始想用krusual算法去做,但想着之前做了那么多的最小生成树,这次换一种算法吧!结果,另一种算法不熟悉,弄得我生不如死啊!我是用Dijkstra算法做的。
不过也不算Dijkstra算法吧,算是一种变形,不过这种变形,有点不太像它本身的作用了。
思 路 :1.从起点开始,我们得到该点到到任意点的距离,取最短。---> 2.用一个数ans将这个距离保存起来,如果还有比它小的数则替换掉。---> 3.松弛操作:改变接下来保存距离数
的大小 ---> 循环前面几步,知道走到终点即可。
题目收获 : 每一次求最短的有效方法。
void Dijkstra() { for(int i = 1; i <= n; i++) d[i] = DNF;//每个值全为无穷。 d[1] = 0;//起始点为0; memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++) { //cout << "***********************" << "\n" <<endl; int x; double m = DNF; for(int y = 1; y <= n; y++)//求最小的权; if(!vis[y] && d[y] <= m) m = d[x=y]; //cout << "X:" << x << endl; vis[x] = 1; if(ans < d[x] && d[x]!= DNF) { ans = d[x];//记录最小权,每次替换。 //printf("DNS:%f\n",ans); } if(x == 2) return; for(int y = 1; y <= n; y++) if(!vis[y]) { d[y] = min(d[y], w[x][y]);//松弛操作。得到当前驱节点到下一任意节点的距离。 } } }
Forgger
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。