noip2012考前水的一些渣代码

Function hash(S:String):longint;
 var
  i:longint;
   begin
   hash:=0;
   for i:=1 to length(s) do
     hash:=(hash*131+ord(s[i])) and $FFFFFFFF;
   hash:=hash and $7fffffff;
 end;
 
 Procedure getpi;
 var
  k,i:longint;
   begin
   pi[1]:=0;
   k:=0;
   for i:=2 to m do
     begin
     while (k>0) and (b[i]<>b[k+1]) do k:=pi[k];
     if b[i]=b[k+1] then inc(k);
     pi[i]:=k;
   end;
 end;
 
 Procedure KMP;
 var
  k,i:longint;
   begin
   k:=0;
   for i:=1 to n do
     begin
     while (k>0) and (b[k+1]<>a[i]) do k:=pi[k];
     if b[k+1]=a[i] then inc(k);
     if k=m then writeln('From ',i-m+1,' to ',i);
   end;
 end;
 
 //前向星+slf优化
 {
 Author:HT
 Date:2012/11/09
 }
 
 Procedure Spfa(ST:longint);
 var
  i,close,open,x:longint;
  v:array[0..30002] of boolean;
  q:array[0..30002] of longint;
   begin
   fillchar(dt,sizeof(dt),$7f);
   dt[st]:=0;
   fillchar(v,sizeof(v),false);
   q[1]:=st;
   v[st]:=true;
   close:=0;
   open:=1;
   while close<>open do
     begin
     close:=close mod n+1;
     x:=q[close];
     for i:=pre[x] to tail[x] do
       if dt[b[i].ed]>dt[x]+b[i].w then
         begin
         dt[b[i].ed]:=dt[x]+b[i].w;
         if not v[b[i].ed] then
           begin
           v[b[i].ed]:=true;
           if dt[b[i].ed]<=dt[x] then//SLF
             begin
             q[close]:=b[i].ed;
             close:=close-1;
             if close<1 then close:=n;
           end else
             begin
             open:=open mod n +1;
             q[open]:=b[i].ed;
           end;
         end;
       end;
     v[x]:=false;
   end;
 end;
 
 Procedure qsort(l,r:longint);
 var
  i,j:longint;
  x,y:rec;
   begin
   i:=l;j:=r;x:=b[(l+r) div 2];
     repeat
     while b[i].st<x.st do inc(i);
     while b[j].st>x.st do dec(j);
     if i<=j then
       begin
       y:=b[i];
       b[i]:=b[j];
       b[j]:=y;
       inc(i);
       dec(j);
     end;
   until i>j;
   if l<j then qsort(l,j);
   if i<r then qsort(i,r);
 end;
 
 Procedure lsh;
 var
  i:longint;
   begin
   fillchar(pre,sizeof(pre),$7f);
   fillchar(tail,sizeof(tail),0);
   for i:=1 to 2*m do
     begin
     if pre[b[i].st]>1e8 then
       pre[b[i].st]:=i;
     tail[b[i].st]:=i;
   end;
 end;
 
   begin
   fopen;
   readln(n,m,ts,te);
   for i:=1 to m do
     begin
     with b[i*2] do
       readln(st,ed,w);
     with b[i*2-1] do
       begin
       st:=b[i*2].ed;
       ed:=b[i*2].st;
       w:=b[i*2].w;
     end;
   end;
   qsort(1,2*m);
   lsh;
   spfa(ts);
   writeln(dt[te]);
   fclose;
 end.
 
 //--------------------------------------------
 //heap
 Procedure swap(var a,b:longint);
 var
  y:longint;
   begin
   y:=a;a:=b;b:=y;
 end;
 
 Procedure down(P:longint);
 var
  o:longint;
   begin
   //writeln('down(',p,') top=',top);
   if p*2>top then exit;
   o:=p;
   if p*2=top then begin
     if a[p]>a[p*2] then swap(a[p],a[p*2])
   end else
     begin
     if a[o]>a[p*2] then o:=p*2;
     if a[o]>a[p*2+1] then o:=p*2+1;
     swap(a[p],a[o]);
     if o<>p then down(o);
   end;
 end;
 
 Procedure up(P:longint);
   begin
   while p>1 do
     if a[p]<a[p div 2] then 
       begin
       a[p div 2]:=a[p];
       p:=p div 2
     end else break;
 end;
 
 Procedure insert(P:longint);
   begin
   inc(top);
   a[top]:=p;
   up(top);
 end;
 
 Function Delete:longint;
   begin
   delete:=a[1];
   swap(a[1],a[top]);
   dec(top);
   down(1);
 end;
原文地址:https://www.cnblogs.com/htfy/p/2763191.html