[POJ2253 Frogger]

[题目来源]:POJ2253

[关键字]:最短路径

[题目大意]:给出一个图,求从一号点到第二号点所走过的路径中最长的一条最短可以是多少。

//=====================================================================================================

[分析]:其实就是最短路径的变形。只需把松弛条件改为d[j] > max(d[p],map[p,j])p为新加入的点,j为可更新的点,map[p,j]为p到j的路径长度,d[p]为到p所走过的最大路径。

[代码]:

View Code
 1 {
2 PROB:POJ2253
3 DATE:2011\10\15
4 }
5 program project1;
6 var
7 n, tc: longint;
8 x, y: array[0..210] of longint;
9 map: array[0..210,0..210] of real;
10 d: array[0..210] of real;
11 b: array[0..210] of boolean;
12
13 procedure init;
14 var
15 i, j: longint;
16 begin
17 readln(n);
18 if n = 0 then halt;
19 for i := 1 to n do
20 readln(x[i],y[i]);
21 readln;
22 fillchar(map,sizeof(map),0);
23 for i := 1 to n do
24 for j := 1 to n do
25 map[i,j] := sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j]));
26 end;
27
28 function max(x, y: real):real;
29 begin
30 if x > y then exit(x) else exit(y);
31 end;
32
33 procedure dij;
34 var
35 i, j, p: longint;
36 min: real;
37 begin
38 fillchar(d,sizeof(d),100);
39 fillchar(b,sizeof(b),false);
40 d[1] := 0;
41 for i := 1 to n do
42 begin
43 min := maxlongint;
44 for j := 1 to n do
45 if (not b[j]) and (min > d[j]) then
46 begin
47 min := d[j];
48 p := j;
49 end;
50 b[p] := true;
51 for j := 1 to n do
52 if (not b[j]) and (map[p,j] <> 0) then
53 if d[j] > max(map[p,j],d[p]) then d[j] := max(map[p,j],d[p]);
54 end;
55 end;
56
57 begin
58 // assign(input,'d:\1.in');reset(input);
59 // assign(output,'d:\1.out');rewrite(output);
60 tc := 0;
61 while 1 = 1 do
62 begin
63 inc(tc);
64 init;
65 dij;
66 writeln('Scenario #',tc);
67 writeln('Frog Distance = ',d[2]:0:3);
68 writeln;
69 end;
70 // close(input);
71 // close(output);
72 end.



你可能感兴趣的:(poj)