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

 
 
 
题解:
1.最短路的变形:把dis[i]从原来的记录最短距离 变为 记录不同路径上最大边权中的最小值。
2.利用dijkstra算法时,每次松弛都是选取dis的最小值。
 
 
 
代码如下:
 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)   //dis[i]为经过i点的路径中边权最大值的最小值。
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 }
View Code

 

 

posted on 2017-09-26 10:59  h_z_cong  阅读(337)  评论(0编辑  收藏  举报

导航