POJ2253 Frogger 【Dijkstra】

Frogger

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26417   Accepted: 8592

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

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

题意:从起点到终点有多条线路,找出所有线路中最大边最小的值。

题解:Dijkstra,dis数组存取当前点到原点的“割”;

#include <stdio.h>
#include <math.h>
#include <string.h>

#define maxn 202
#define inf 0x3f3f3f3f

int n;
struct Node {
	int x, y;
} Stone[maxn];
double map[maxn][maxn], dis[maxn];
bool vis[maxn];

double min(double a, double b) {
	return a < b ? a : b;
}

double max(double a, double b) {
	return a > b ? a : b;
}

double calDis(int i, int j) {
	double x = Stone[i].x - Stone[j].x;
	double y = Stone[i].y - Stone[j].y;
	return sqrt(x * x + y * y);
}

int getNext() {
	int pos = -1, i;
	double val = inf;
	for(i = 0; i < n; ++i)
		if(!vis[i] && dis[i] < val) {
			val = dis[i]; pos = i;
		}
	return pos;
}

void Dijkstra(int start, int end) {
	int u = start, v, i;
	for(i = 0; i < n; ++i) {
		vis[i] = 0; dis[i] = inf;
	}
	dis[u] = 0.0;
	while(u != -1) {
		vis[u] = 1;
		for(i = 0; i < n; ++i)
			dis[i] = min(dis[i], max(map[u][i], dis[u]));
		u = getNext();
		if(u == end) return;
	}
}

int main() {
	// freopen("stdin.txt", "r", stdin);
	int i, j, cas = 1;
	while(scanf("%d", &n), n) {
		for(i = 0; i < n; ++i) {
			scanf("%d%d", &Stone[i].x, &Stone[i].y);
			for(j = 0; j < i; ++j)
				map[i][j] = map[j][i] = calDis(i, j);
			map[i][i] = 0.0;
		}
		Dijkstra(0, 1);
		printf("Scenario #%d\nFrog Distance = %.3lf\n\n", cas++, dis[1]);
	}
	return 0;
}
时间: 2024-10-05 23:58:46

POJ2253 Frogger 【Dijkstra】的相关文章

POJ2253 Frogger 【Floyd】

讲的是,一只雄青蛙要从一个石头到另外一个石头上去找某只雌青蛙,但是这两个石头隔得太远,青蛙跳不过去,所幸,湖面上还有很多其他石头,所以青蛙可以借助别的石头一步一步地跳向那只雌青蛙所在的石头.显然青蛙可能有多种路径,比如其中一条是 2,3,4,2,1 ,它跳了五次,数字代表每次跳的距离也就是路径上相邻两个石头之间的距离,那么这只青蛙的弹跳能力至少是4才能跳过去.在其他的路径中,可能要求青蛙的弹跳是5,是8,是1,是100,等等,这个问题求青蛙需要的最小弹跳能力.其实也就是个最大值中取最小的问题.

HDU2680 Choose the best route 【Dijkstra】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7061    Accepted Submission(s): 2300 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU1874 畅通工程续 【Dijkstra】

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 26970    Accepted Submission(s): 9719 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走

HDU2544 最短路 【Dijkstra】

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31182    Accepted Submission(s): 13456 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

HDOJ 1874 畅通工程续 【dijkstra】

题意:... 策略:最简单的求最短路径. 代码: #include<stdio.h> #include<string.h> #define MAXN 1005 #define INF 0x3f3f3f3f int di[MAXN], vis[MAXN], n, m; int map[MAXN][MAXN]; void dijkstra(int v) { int i, j; memset(vis, 0, sizeof(vis)); di[v] = 0; vis[v] = 1; for

HDU3790 最短路径问题 【Dijkstra】

最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13336    Accepted Submission(s): 4072 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input

POJ2253&amp;ZOJ1942--Frogger【SPFA】单源最短路变形

链接:http://poj.org/problem?id=2253 题意:一个青蛙在一块石头上,看到了另一个青蛙在另一块石头上,它想跳过去找它,如果距离太远它就需要借助别的石头当跳板,两块石头之间的青蛙距离被定义成两块石头之间所有路径中最大跳跃距离的最小值,求两个青蛙之间的青蛙距离. poj2263和它类似,链接:http://poj.org/problem?id=2263 解题报告:Here 这是最短路的变形,每两点之间都有路可以跳,更新最短路的值,权值记录成目前到这一点的最小青蛙距离就行了

HDU2112 HDU Today 【Dijkstra】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14743    Accepted Submission(s): 3471 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候