var
n,m,s,i,j,x,y,z,l,tot :longint;
pre,last,other,len :array[..] of longint;
heap,d,pl :Array[..] of longint; procedure swap(var x,y:longint);
var
z :longint;
begin
z:=x;
x:=y;
y:=z;
end; procedure add(u,v,r:longint);
begin
inc(l);
pre[l]:=last[u];
last[u]:=l;
other[l]:=v;
len[l]:=r;
end; procedure up(x:longint);
var
i :longint;
begin
i:=x;
while (i>>)> do
begin
if (d[heap[i>>]]<=d[heap[i]]) then break;
swap(heap[i],heap[i>>]);
pl[heap[i]]:=i;
pl[heap[i>>]]:=i>>;
i:=i>>;
end;
end; procedure down(x:longint);
var
i,j :longint;
begin
i:=x;
while (i<<)<=tot do
begin
j:=i<<;
if (j+<=tot) and (d[heap[j+]]<d[heap[j]]) then inc(j);
if (d[heap[i]]<=d[heap[j]]) then break;
swap(heap[i],heap[j]);
pl[heap[i]]:=i;
pl[heap[j]]:=j;
i:=j;
end;
end; procedure delete(x:longint);
begin
heap[x]:=heap[tot];
dec(tot);
pl[heap[x]]:=x;
down(x);
end; procedure dijkstra;
var
cur,p,q,i :longint;
begin
for i:= to n do
begin
cur:=heap[];
delete();
p:=last[cur];
while p<> do
begin
q:=other[p];
if d[q]>d[cur]+len[p] then
begin
d[q]:=d[cur]+len[p];
up(pl[q]);
end;
p:=pre[p];
end;
end;
end; begin
//assign(input,'input.txt');reset(input);
//assign(output,'output.txt');rewrite(output);
read(n,m,s);
for i:= to m do
begin
read(x,y,z);
add(x,y,z);
end;
for i:= to n do d[i]:=maxlongint>>;
d[s]:=;
for i:= to n do
begin
heap[i]:=i;
pl[i]:=i;
end;
up(s);
tot:=n;
dijkstra;
for i:= to n do if d[i]=maxlongint>> then d[i]:=;
for i:= to n do write(d[i],' ');
close(input);
close(output);
end.

[dijkstra+heap优化] 模板的更多相关文章

  1. hdu 2544 单源最短路问题 dijkstra+堆优化模板

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. 最短路--dijkstra+优先队列优化模板

    不写普通模板了,还是需要优先队列优化的昂 #include<stdio.h> //基本需要的头文件 #include<string.h> #include<queue&g ...

  3. Dijkstra + 优先队列优化 模板

    #include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ...

  4. dijkstra堆优化模板

    #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #in ...

  5. Luogu P4779 【模板】单源最短路径(标准版)(Dijkstra+堆优化模板)

    qwq dij其实和prim挺像的,prim是找权值最小点,dij是找边, 用一个优先队列就可以在加入边的时候直接排序,避免了每次遍历更新min priority_queue <pair< ...

  6. (模板)poj2387(dijkstra+优先队列优化模板题)

    题目链接:https://vjudge.net/problem/POJ-2387 题意:给n个点(<=1000),m条边(<=2000),求结点n到结点1的最短路. 思路:dijkstra ...

  7. 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛

    传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...

  8. 【最短路算法】Dijkstra+heap和SPFA的区别

    单源最短路问题(SSSP)常用的算法有Dijkstra,Bellman-Ford,这两个算法进行优化,就有了Dijkstra+heap.SPFA(Shortest Path Faster Algori ...

  9. 【模板】Dijkstra的heap优化

    为了将最小费用最大流的spfa优化,决定将spfa换成heap优化的Dijkstra.(dijkstra不能处理负边权) 所以还得现学... 白点表示已经确定最短路径的点. 蓝点表示还未确定最短路径的 ...

随机推荐

  1. Java原始的8中数据类型

    数据类型 大小 范围 默认值 ========   ========  ============================================  =========byte(字节) ...

  2. Excle隐藏及展开列

    当excle文档类目比较多的时候我们希望看第一列和某一列的对应关系可以选择隐藏中间列. 选中要隐藏的列,然后右键-->隐藏即可 需要展开的时候,选中:被隐藏列的前一列和后一列,然后当鼠标在列头( ...

  3. Python之路 day2 初识字典

    #Author:ersa ''' key-value 键值对 字典是无序的,不需要下标,有key 字典的查找.修改.添加.判断.删除 ''' info = { 'stu1101': "Ten ...

  4. GIT ON WINDOWS

    https://help.github.com/articles/generating-an-ssh-key/

  5. wifidog 配置中文说明

    #网关IDGatewayID default#外部网卡ExternalInterface eth0#无线网卡GatewayInterface eth0#无线IPGatewayAddress 192.1 ...

  6. LRU Cache [LeetCode]

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  7. 如何有效使用Project(2)——进度计划的执行与监控

    继上次的的<编制进度计划.保存基准>继续讲解如何对计划进行执行和监控. 计划执行即:反馈实际进度.反馈工作消耗(本文只考虑工时,不考虑成本).提出计划变更请求.如果你的企业实施了专门的PM ...

  8. A:石头剪刀布

    总时间限制: 1000ms 内存限制: 65536kB描述石头剪刀布是常见的猜拳游戏.石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.一天,小A和小B正好在玩石头剪刀布.已知他们的出拳 ...

  9. oracle procedure

    http://www.cnblogs.com/wuhenke/archive/2010/03/20/1690535.html

  10. IIS7配置asp网站

    An error occurred on the server when processing the URL. Please contact the system administrator. If ...