• [dijkstra+heap优化] 模板


      1 var
      2     n,m,s,i,j,x,y,z,l,tot                :longint;
      3     pre,last,other,len                    :array[0..1000050] of longint;
      4     heap,d,pl                            :Array[0..1000050] of longint;
      5     
      6 procedure swap(var x,y:longint);
      7 var
      8     z                                    :longint;
      9 begin
     10     z:=x;
     11     x:=y;
     12     y:=z;
     13 end;
     14     
     15 procedure add(u,v,r:longint);
     16 begin
     17     inc(l);
     18     pre[l]:=last[u];
     19     last[u]:=l;
     20     other[l]:=v;
     21     len[l]:=r;
     22 end;
     23 
     24 procedure up(x:longint);
     25 var
     26     i                                    :longint;
     27 begin
     28     i:=x;
     29     while (i>>1)>0 do
     30     begin
     31         if (d[heap[i>>1]]<=d[heap[i]]) then break;
     32         swap(heap[i],heap[i>>1]);
     33         pl[heap[i]]:=i;
     34         pl[heap[i>>1]]:=i>>1;
     35         i:=i>>1;
     36     end;
     37 end;
     38 
     39 procedure down(x:longint);
     40 var
     41     i,j                                    :longint;
     42 begin
     43     i:=x;
     44     while (i<<1)<=tot do
     45     begin
     46         j:=i<<1;
     47         if (j+1<=tot) and (d[heap[j+1]]<d[heap[j]]) then inc(j);
     48         if (d[heap[i]]<=d[heap[j]]) then break;
     49         swap(heap[i],heap[j]);
     50         pl[heap[i]]:=i;
     51         pl[heap[j]]:=j;
     52         i:=j;
     53     end;
     54 end;
     55 
     56 procedure delete(x:longint);
     57 begin
     58     heap[x]:=heap[tot];
     59     dec(tot);
     60     pl[heap[x]]:=x;
     61     down(x);
     62 end;
     63 
     64 procedure dijkstra;
     65 var
     66     cur,p,q,i                            :longint;
     67 begin
     68     for i:=1 to n do
     69     begin
     70         cur:=heap[1];
     71         delete(1);
     72         p:=last[cur];
     73         while p<>0 do
     74         begin
     75             q:=other[p];
     76             if d[q]>d[cur]+len[p] then
     77             begin
     78                 d[q]:=d[cur]+len[p];
     79                 up(pl[q]);
     80             end;
     81             p:=pre[p];
     82         end;
     83     end;
     84 end;
     85     
     86 begin
     87     //assign(input,'input.txt');reset(input);
     88     //assign(output,'output.txt');rewrite(output);
     89     read(n,m,s);
     90     for i:=1 to m do
     91     begin
     92         read(x,y,z);
     93         add(x,y,z);
     94     end;
     95     for i:=1 to n do d[i]:=maxlongint>>1;
     96     d[s]:=0;
     97     for i:=1 to n do 
     98     begin
     99         heap[i]:=i;
    100         pl[i]:=i;
    101     end;
    102     up(s);
    103     tot:=n;
    104     dijkstra;
    105     for i:=1 to n do if d[i]=maxlongint>>1 then d[i]:=2147483647;
    106     for i:=1 to n do write(d[i],' ');
    107     close(input);
    108     close(output);
    109 end.
  • 相关阅读:
    服务返返回状态码详解
    LeetCode#28 Implement strStr()
    LeetCode#58 Length of Last Word
    LeetCode#66 Plus One
    spooling技术
    文件的打开与关闭
    DMA方式与通道方式
    中断向量、向量中断、向量地址
    中断响应优先级和中断处理优先级
    I/O接口
  • 原文地址:https://www.cnblogs.com/zoewilly/p/6020677.html
Copyright © 2020-2023  润新知