P3408: [Usaco2009 Oct]Heat Wave 热浪

水题,裸的最短路。

const maxn=6200001;
type
  link=^node;
  node=record
    t,d:longint;
    f:link;
  end;
var n,m,s,i,j,u,v,w,max:longint;
adj:array[0..3000] of link;
f:array[0..1000000] of longint;
d,val:array[0..3000] of longint;
went:array[0..3000] of boolean;
procedure insert(f,t,d:longint);
var p:link;
begin
  new(p);
  p^.f:=adj[f];
  p^.t:=t;
  p^.d:=d;
  adj[f]:=p;
end;
procedure spfa(s:longint);
var l,r,now,i:longint;
p:link;
begin
  for i:=1 to n do
    d[i]:=maxn;
  fillchar(went,sizeof(went),true);
  l:=1; r:=1; f[1]:=s; d[s]:=0; went[s]:=false;
  while l<=r do
    begin
      now:=f[l];
      p:=adj[now];
      while p<>nil do
        begin
          if d[p^.t]>d[now]+p^.d then
            begin
              d[p^.t]:=d[now]+p^.d;
              if went[p^.t] then
                begin
                  went[p^.t]:=false;
                  inc(r);
                  f[r]:=p^.t;
                end;
            end;
          p:=p^.f;
        end;
      went[now]:=true;
      inc(l);
    end;
end;
begin
  readln(n,m,s);
  for i:=1 to m do
    begin
      readln(u,v,w);
      insert(u,v,w);
      //insert(v,u,w);
    end;
  for i:=1 to n do
    if i<>s then
      begin
        spfa(i);
        val[i]:=d[s];
      end;
  spfa(s);
  for i:=1 to n do
    if i<>s then
      begin
        inc(val[i],d[i]);
        if val[i]>max then max:=val[i];
      end;
  writeln(max);
end.

 (转载请注明出处:http://www.cnblogs.com/Kalenda/)

 

posted @ 2015-09-16 18:01  LovelyMonster丶  阅读(258)  评论(0编辑  收藏  举报