码迷,mamicode.com
首页 > 其他好文 > 详细

P3408: [Usaco2009 Oct]Heat Wave 热浪

时间:2015-09-16 20:03:55      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

水题,裸的最短路。

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.

 

P3408: [Usaco2009 Oct]Heat Wave 热浪

标签:

原文地址:http://www.cnblogs.com/Kalenda/p/4814045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!