I and OI
Past...

简单的最短路.要用堆优化的dijkstra.当复习了.

code:

/************************************************************** 
    Problem: 2100
    User: exponent 
    Language: Pascal 
    Result: Accepted 
    Time:572 ms 
    Memory:7356 kb 
****************************************************************/ 
  
type  edge=record
      v,w,n:longint; 
end; 
const maxm=400000; 
      maxn=100001; 
var   e:array[0..maxm] of edge; 
      h:array[0..maxm] of longint; 
      head,d:array[0..maxn] of longint; 
      vis:array[0..maxn] of boolean; 
      n,m,home,pa,pb,u,v,w,i,ha,hb,ab,cnt,size:longint; 
  
      procedure add(u,v,w:longint); 
      begin
            inc(cnt); 
            e[cnt].v:=v; 
            e[cnt].w:=w; 
            e[cnt].n:=head[u]; 
            head[u]:=cnt; 
      end; 
  
      procedure swap(a,b:longint); 
      var   temp:longint; 
      begin
            temp:=h[a]; 
            h[a]:=h[b]; 
            h[b]:=temp; 
      end; 
  
      procedure down(k:longint); 
      var   p:longint; 
      begin
            while k<=size>>1 do
            begin
                  p:=k<<1; 
                  if (p<size)and(d[h[p]]>d[h[p+1]]) then inc(p); 
                  if d[h[k]]>d[h[p]] then
                    begin swap(k,p); k:=p; end
                  else break; 
            end; 
      end; 
  
      procedure up(k:longint); 
      begin
            while (k>1)and(d[h[k>>1]]>d[h[k]]) do
            begin swap(k,k>>1); k:=k>>1; end; 
      end; 
  
      procedure ins(new:longint); 
      begin
            inc(size); 
            h[size]:=new; 
            up(size); 
      end; 
  
      procedure deltop; 
      begin
            swap(1,size); 
            dec(size); 
            down(1); 
      end; 
  
      function dijk(s,t:longint):longint; 
      var   u,v,p:longint; 
      begin
            fillchar(vis,sizeof(vis),0); 
            fillchar(d,sizeof(d),127); 
            size:=0; 
            d[s]:=0; 
            ins(s); 
            while size>0 do
            begin
                  u:=h[1]; 
                  deltop; 
                  if vis[u] then continue; 
                  vis[u]:=true; 
                  p:=head[u]; 
                  while p<>0 do
                  begin
                        v:=e[p].v; 
                        if d[v]>d[u]+e[p].w then
                        begin
                              d[v]:=d[u]+e[p].w; 
                              ins(v); 
                        end; 
                        p:=e[p].n; 
                  end; 
            end; 
            exit(d[t]); 
      end; 
  
      function min(a,b:longint):longint; 
      begin
            if a>b then exit(b); exit(a); 
      end; 
  
begin
      readln(m,n,home,pa,pb); 
      for i:=1 to m do
      begin
            readln(u,v,w); 
            add(u,v,w); 
            add(v,u,w); 
      end; 
  
      ha:=dijk(home,pa); 
      hb:=dijk(home,pb); 
      ab:=dijk(pa,pb); 
      writeln(min(ha,hb)+ab); 
end.
posted on 2011-08-14 13:31  exponent  阅读(276)  评论(0编辑  收藏  举报