POJ2253 Frogger —— 最短路变形

题目链接:http://poj.org/problem?id=2253

Frogger

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 49409   Accepted: 15729

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

题解:

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
13 #define ms(a,b) memset((a),(b),sizeof((a)))
14 using namespace std;
15 typedef long long LL;
16 const double EPS = 1e-8;
17 const int INF = 2e9;
18 const LL LNF = 9e18;
19 const int MOD = 1e9+7;
20 const int MAXN = 1e3+10;
21
22 int n;
23
24 struct edge
25 {
26     double w;
27     int to, next;
28 }edge[MAXN*MAXN];
29 int cnt, head[MAXN];
30
31 void addedge(int u, int v, double w)
32 {
33     edge[cnt].to = v;
34     edge[cnt].w = w;
35     edge[cnt].next = head[u];
36     head[u] = cnt++;
37 }
38
39 void init()
40 {
41     cnt = 0;
42     memset(head, -1, sizeof(head));
43 }
44
45 double dis[MAXN];
46 bool vis[MAXN];
47 void dijkstra(int st)
48 {
49     memset(vis, 0, sizeof(vis));
50     for(int i = 1; i<=n; i++)
51         dis[i] = (i==st?0:INF);
52
53     for(int i = 1; i<=n; i++)
54     {
55         int k;
56         double minn = INF;
57         for(int j = 1; j<=n; j++)
58             if(!vis[j] && dis[j]<minn)
59                 minn = dis[k=j];
60
61         vis[k] = 1;
62         for(int j = head[k]; j!=-1; j = edge[j].next)
63             if(!vis[edge[j].to])
64                 dis[edge[j].to] = min(dis[edge[j].to], max(dis[k], edge[j].w) );
65     }
66 }
67
68 int x[MAXN], y[MAXN];
69 int main()
70 {
71     int kase = 0;
72     while(scanf("%d", &n) && n)
73     {
74         init();
75         for(int i = 1; i<=n; i++)
76             scanf("%d%d", &x[i], &y[i]);
77         for(int i = 1; i<=n; i++)
78             for(int j = 1; j<=n; j++)
79                 addedge(i, j, sqrt( (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])) );
80
81         dijkstra(1);
82         printf("Scenario #%d\n", ++kase);
83         printf("Frog Distance = %.3f\n\n", dis[2]);
84     }
85 }

时间: 2024-11-09 06:11:28

POJ2253 Frogger —— 最短路变形的相关文章

poj2253 Frogger(最短路变型或者最小生成树)

1 /* 2 题意:就是源点到终点有多条的路径,每一条路径中都有一段最大的距离! 3 求这些路径中最大距离的最小值! 4 5 Dijkstra, Floyd, spfa都是可以的!只不过是将松弛的条件变一下就行了! 6 7 想了一下,这道题用最小生成树做也可以啊,图总是连通的嘛!所以建一棵最小 8 生成树,然后dfs一下,从源点1,到终点2的路径上,查找边长最大的路径! 9 附上代码..... 10 */ 11 #include<iostream> 12 #include<cstdio&

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

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

POJ2253——Frogger(Floyd变形)

Frogger DescriptionFreddy 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 swimm

POJ2263&amp;ZOJ1952--Heavy Cargo【Floyd】多源最短路变形

链接:http://poj.org/problem?id=2263 题意:有n个点,m条路,每条路双向的,现在卡车从某点到另一点,卡车的承载无上限,但是马路的承载有上限,问卡车应该承载多少才不会压坏马路. poj2253和它类似,链接:http://poj.org/problem?id=2253 解题报告:Here 就是在两点之间找一条路径,使路径中权值最小的那条边的权值最大,edge数组记录当前路径中最小权值边的权值 #include<cstring> #include<string&

URAL 1934 Black Spot --- 简单最短路变形

边权为1,在维护最短路的同时维护p值最小,我直接存的(1-p),即不遇见的概率,要使得这个值最大. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #includ

UESTC 915 方老师的分身II --最短路变形

即求从起点到终点至少走K条路的最短路径. 用两个变量来维护一个点的dis,u和e,u为当前点的编号,e为已经走过多少条边,w[u][e]表示到当前点,走过e条边的最短路径长度,因为是至少K条边,所以大于K条边的当做K条边来处理就好了.求最短路的三个算法都可以做,我这里用的是SPFA,比较简洁. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #incl

zoj 1655 Transport Goods (最短路变形)

Transport Goods Time Limit: 2 Seconds      Memory Limit: 65536 KB The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the

POJ2253 Frogger 【Floyd】

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

zoj1655 最短路变形

题意:HERO过的首都需要货物,需要从其他的城市吧货物送到首都,每条道路都会需要消耗一定比例的货物,问最多能送多少货物到首都. 思路:如果每个点的比例是1,到达首都的比例就是经过的路径的(1-消耗比)的乘积,反正是无向的,所以可以反过来推,首都的货物比是1,而到达每座 城市的货物就是所经过的路径(1-消耗比)的乘积,则由此可见,我们可以求首都到任意城市的最大比值:最后把每个点的最大比值乘以每个点的货物加起来 即是结果. #include<stdio.h> #include<string.