I and OI
Past...

1.Dijkstra算法.(O(N^2)的算法就不写了,贴个堆优化的Dijkstra.)

type  node=record
      u,d:longint;
end;
const maxn=30001;
      maxm=400001;
      oo=1<<30;
var   n,m,s,t,cnt,size,u,v,w,i,j:longint;
      e:array[0..maxm] of record
                          v,n,w:longint;
                          end;
      l,d:array[0..maxn] of longint;
      vis:array[0..maxn] of boolean;
      h:array[0..maxm] of node;

      procedure swap(var a:longint; b:longint);
      var   temp:node;
      begin
            temp:=h[a];
            h[a]:=h[b];
            h[b]:=temp;
            a:=b;
      end;

      procedure ins(new:node);
      var   k:longint;
      begin
            inc(size);
            h[size]:=new;
            k:=size;
            while (k>1)and(h[k>>1].d>h[k].d) do swap(k,k>>1);
      end;

      procedure deltop;
      var   k,p:longint;
      begin
            h[1]:=h[size];
            dec(size);
            k:=1;
            while k<=size>>1 do
            begin
                  p:=k<<1;
                  if (p<size)and(h[p].d>h[p+1].d) then inc(p);
                  if h[k].d>h[p].d then swap(k,p)
                  else break;
            end;
      end;

      procedure add(u,v,w:longint);
      begin
            inc(cnt);
            e[cnt].v:=v;
            e[cnt].w:=w;
            e[cnt].n:=l[u];
            l[u]:=cnt;
      end;

      procedure dijkstra;
      var   u,v,p,w:longint;
            new,now:node;
      begin
            fillchar(d,sizeof(d),127);
            d[s]:=0;
            new.u:=s;
            new.d:=0;
            ins(new);
            while size>0 do
            begin
                  now:=h[1];
                  u:=now.u;
                  w:=now.d;
                  if u=t then break; //important
                  deltop;
                  if vis[u] then continue;
                  vis[u]:=true;
                  p:=l[u];
                  while p<>0 do
                  begin
                        v:=e[p].v;
                        if d[u]+e[p].w<d[v] then
                        begin
                              d[v]:=d[u]+e[p].w;
                              new.d:=d[v];
                              new.u:=v;
                              ins(new);
                        end;
                        p:=e[p].n;
                  end;
            end;
            writeln(d[t]);
      end;

begin
      assign(input,'sssp.in'); reset(input);
      assign(output,'hd.out'); rewrite(output);
      readln(n,m);
      for i:=1 to m do
      begin
            readln(u,v,w);
            add(u,v,w);
            add(v,u,w);
      end;
      s:=1;
      t:=n;
      dijkstra;
      close(output);
end.

 

2.SPFA算法(Bellman-Ford算法的改进版)

type  enode=record
      v,w,n:longint;
end;
const oo=2139062143;
var   e:array[0..310000] of enode;
      h,d,q:array[0..31000] of longint;
      vis:array[0..31000] of boolean;
      n,m,i,x,y,z,head,tail,s,t,u,v,p,cnt:longint;

      procedure add(x,y,z:longint);
      begin
            inc(cnt);
            e[cnt].v:=y;
            e[cnt].w:=z;
            e[cnt].n:=h[x];
            h[x]:=cnt;
      end;

begin
      assign(input,'sssp.in'); reset(input);
      assign(output,'spfa.out'); rewrite(output);

      readln(n,m);
      for i:=1 to m do
      begin
            readln(x,y,z);
            add(x,y,z);
            add(y,x,z);
      end;
      s:=1; t:=n;
      fillchar(d,sizeof(d),127);
      fillchar(vis,sizeof(vis),0);
      head:=0;
      tail:=1;
      q[1]:=s;
      d[s]:=0;
      vis[s]:=true;
      while head<>tail do
      begin
            head:=head mod n+1;
            u:=q[head];
            vis[u]:=false;
            p:=h[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;
                        if not vis[v] then
                        begin
                              vis[v]:=true;
                              tail:=tail mod n+1;
                              q[tail]:=v;
                        end;
                  end;
                  p:=e[p].n;
            end;
      end;
      if d[t]=oo then writeln(-1)
      else writeln(d[t]);

      close(output);
end.

3.Floyd算法.

procedure floyd;
for k:=1 to n do
   for i:=1 to n do
      for j:=1 to n do
      dis[i,j]:=min(dis[i,j],dis[i,k]+dis[k,j]);
posted on 2011-08-07 20:11  exponent  阅读(276)  评论(0编辑  收藏  举报