【图论】【poj 2253】Frogger

问题

给定你一个有向图,让你求两点之间所有可行路径上的最大值之中的最小值。多组数据

分析

经典的floyd算法的变形,a[i,j]表示i,j两点之间所有可行路径上的最大值之中的最小值,我们可以的到这样的方程

a[i,j]:=min{a[i,j],max{a[i,k],a[k,j]}};很好理解。

code

program liukee;
var
  a:array[0..201,0..201] of double;
  n,tt:longint;

procedure init;
var
  x,y:array[0..202] of double;
  i,j:longint;
begin
  fillchar(x,sizeof(x),0);
  fillchar(y,sizeof(y),0);
  for i:=1 to n do
    readln(x[i],y[i]);
  for i:=1 to n do
    for j:=1 to n do
	  if i<>j then
	  a[i,j]:=sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j]));
  for i:=1 to n do
    for j:=1 to n do
	  if a[i,j]=0 then a[i,j]:=maxlongint>>1;
end;

function max(a,b:double):double;
begin
  if a>b then exit(a) else exit(b);
end;

procedure doit;
var
  i,j,k:longint;
begin
 for k:=1 to n do
   for i:=1 to n do
     for j:=1 to n do
	   if i<>j then
		 if (k<>i)and(k<>j) then
		    if a[i,j]>max(a[i,k],a[k,j]) then a[i,j]:=max(a[i,k],a[k,j]);
  writeln('Frog Distance = ',a[1,2]:0:3);
  writeln;
end;

begin
  readln(n);
  tt:=0;
  while n<>0 do
  begin
    inc(tt);
    fillchar(a,sizeof(a),0);
    init;
    writeln('Scenario #',tt);
    doit;
    readln;
    readln(n);
  end;
end.

反思

一定要先循环k!!!!!

要联想到合适的经典算法,floyd求最值,用到的是一种动态规划的思想。

posted @ 2011-03-06 19:15  liukee  阅读(191)  评论(0编辑  收藏  举报