pku3615 Cow Hurdles

给一个n个顶点的图(怎么最近的随笔都拿这句话开头^_^),Q条询问,(x,y),查询从x到y的路径上的最大边的最小值。(Q<=40000)

最大最小问题99%都是二分答案,这道题却偏偏是那1%.

用floyd,最后的关键语句变成f[i,j]=min(f[i,j],max(f[i,k],f[k,j]))即可。算法执行后f[i,j]就是问题(i,j)的答案。

View Code
 1 program pku3615(input,output);
2 var
3 f : array[0..400,0..400] of longint;
4 n,m : longint;
5 q : longint;
6 function min(aa,bb :longint ):longint;
7 begin
8 if aa<bb then
9 exit(aa);
10 exit(bb);
11 end; { min }
12 function max(aa,bb :longint ):longint;
13 begin
14 if aa>bb then
15 exit(aa);
16 exit(bb);
17 end; { max }
18 procedure init;
19 var
20 i,j : longint;
21 xx,yy,ww : longint;
22 begin
23 fillchar(f,sizeof(f),0);
24 for i:=1 to 300 do
25 for j:=1 to 300 do
26 if i<>j then
27 f[i,j]:=maxlongint>>1;
28 readln(n,m,q);
29 for i:=1 to m do
30 begin
31 readln(xx,yy,ww);
32 f[xx,yy]:=min(f[xx,yy],ww);
33 end;
34 end; { init }
35 procedure main;
36 var
37 i,j,k : longint;
38 begin
39 for k:=1 to n do
40 for i:=1 to n do
41 if (i<>k) then
42 for j:=1 to n do
43 if (i<>j)and(j<>k) then
44 f[i,j]:=min(f[i,j],max(f[i,k],f[k,j]));
45 end; { main }
46 procedure print;
47 var
48 i : longint;
49 xx,yy : longint;
50 begin
51 for i:=1 to q do
52 begin
53 readln(xx,yy);
54 if f[xx,yy]<maxlongint>>1 then
55 writeln(f[xx,yy])
56 else
57 writeln(-1);
58 end;
59 end; { print }
60 begin
61 init;
62 main;
63 print;
64 end.



posted @ 2012-03-21 12:04  Codinginging  阅读(355)  评论(0编辑  收藏  举报