洛谷 4779

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define N 100010
#define rg register
using namespace std;
int n,m,s,tot,last[N],dis[N];
struct edge{int to,pre,dis;}e[];
struct node{
int poi,dis;
bool operator <(const node& rhs)const {return dis>rhs.dis;}
};
priority_queue<node>q;
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void dijkstra(int x){
for(rg int i=;i<=n;i++) dis[i]=1e9+;
q.push((node){x,dis[x]=});
while(!q.empty()){
node tmp=q.top(); q.pop();
int now=tmp.poi;
if(dis[now]!=tmp.dis) continue;
for(rg int i=last[now],to;i;i=e[i].pre)if(dis[to=e[i].to]>dis[now]+e[i].dis){
dis[to]=dis[now]+e[i].dis;
q.push((node){to,dis[to]});
}
}
}
int main(){
n=read(); m=read(); s=read();
for(rg int i=;i<=m;i++){
int u=read(),v=read();
e[++tot]=(edge){v,last[u],read()}; last[u]=tot;
}
dijkstra(s);
for(rg int i=;i<=n;i++) printf("%d ",dis[i]);
return ;
}

手写堆的版本

 #include<cstdio>
#include<algorithm>
#define N 100010
#define rg register
#define LL long long
using namespace std;
int n,m,s,tot,fa,son,last[N],pos[N],dis[N];
struct edge{int to,pre,dis;}e[];
struct heap{int poi,dis;}h[N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void up(int x){
while((fa=x>>)&&h[fa].dis>h[x].dis)
swap(h[x],h[fa]),swap(pos[h[x].poi],pos[h[fa].poi]),x=fa;
}
inline void down(int x){
while((son=x<<)<=tot){
if(h[son].dis>h[son+].dis&&son<tot) son++;
if(h[son].dis<h[x].dis) swap(h[son],h[x]),swap(pos[h[son].poi],pos[h[x].poi]),x=son;
else return;
}
}
inline void dijkstra(int x){
for(rg int i=;i<=n;i++) dis[i]=1e9+;
h[pos[x]=tot=]=(heap){x,dis[x]=};
while(tot){
int now=h[].poi; pos[h[tot].poi]=; h[]=h[tot--]; if(tot) down();
for(rg int i=last[now],to;i;i=e[i].pre)if(dis[to=e[i].to]>dis[now]+e[i].dis){
dis[to]=dis[now]+e[i].dis;
if(!pos[to]) h[pos[to]=++tot]=(heap){to,dis[to]};
else h[pos[to]].dis=dis[to];
up(pos[to]);
}
}
}
int main(){
n=read(); m=read(); s=read();
for(rg int i=;i<=m;i++){
int u=read(),v=read();
e[++tot]=(edge){v,last[u],read()}; last[u]=tot;
}
dijkstra(s);
for(rg int i=;i<=n;i++) printf("%d ",dis[i]);
return ;
}

【模板】dijkstra的更多相关文章

  1. 基础最短路(模板 dijkstra)

    Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多 ...

  2. 模板 Dijkstra+链式前向星+堆优化(非原创)

    我们首先来看一下什么是前向星.   前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序, 并记录下以某个点为起点的所有边在数组中的起始位置和 ...

  3. [模板] dijkstra (堆优化)

    复杂度O(mlogn) 输入起点s,可以得到从起点到各点的最短路距离数组dis[i] 过程: 1.初始化:清空标记数组,初始化距离数组设为inf,起点距离设为0,开优先队列,搜索起点 2.搜索:取出队 ...

  4. POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)

    题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  5. Dijkstra和Prim算法的区别

    Dijkstra和Prim算法的区别 1.先说说prim算法的思想: 众所周知,prim算法是一个最小生成树算法,它运用的是贪心原理(在这里不再证明),设置两个点集合,一个集合为要求的生成树的点集合A ...

  6. [笔记-图论]Dijkstra

    用于求正权有向图 上的 单源最短路 优化后时间复杂度O(mlogn) 模板 // Dijkstra // to get the minumum distance with no negtive way ...

  7. CSP-S需备模板大全

    CSP-S需备模板大全 谨以此文祝愿自己\(CSP-S\,\,2019\,\,\color{red}{RP++!!}\) 算法 二分 while(l<r) { int mid=(l+r+1)&g ...

  8. ACM模板合集

    写在前面: 第一年小白拿铜牌,第二年队友出走,加上疫情原因不能回校训练导致心底防线彻底崩盘,于是选择退役. 自从退役之后,一直想我打了那么久的ACM,什么也没留下觉得很难受,突然想到我打ACM的时候, ...

  9. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  10. Dijkstra 模板 最短路

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents ------------------------------------------ ...

随机推荐

  1. [Unity UGUI]ScrollRect效果大全

    UGUI各种优化效果 本文所实现的UGUI效果需求如下: - 支持缩放滑动效果 - 支持动态缩放循环加载 - 支持大数据固定Item复用加载 - 支持不用Mask遮罩无限循环加载 - 支持Object ...

  2. Git新建本地分支与远程分支关联问题:git branch --set-upstream【转】

    本文转载自:http://blog.csdn.net/netwalk/article/details/21088405 Git新建本地分支与远程分支关联问题:git branch --set-upst ...

  3. ASP.NET Web Projects

    https://msdn.microsoft.com/en-us/library/ywdtth2f.aspx The topics in this section describe how to cr ...

  4. sqlserver 触发器实例代码

    定义: 何为触发器?在SQL Server里面也就是对某一个表的一定的操作,触发某种条件,从而执行的一段程序.触发器是一个特殊的存储过程. 常见的触发器有三种:分别应用于Insert , Update ...

  5. openstack instance resize to

    Icehouse resize No valid host was found Hi all!! We're currently experimenting an error that's it's ...

  6. Java中static final 与 final 的区别(转载)

    转自:http://advance0683.iteye.com/blog/1107732 Java中static final 与 final 的区别: 例子: Java代码 import java.u ...

  7. php实现下载

    PHP实现下载文件的两种方法.分享下,有用到的朋友看看哦. 方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php /** * 下载文件 * ...

  8. SceneView 追踪选择目标

    在编辑器的Scene视图中追踪选择目标,调试动作用 SceneView这个类没有说明文档比较蛋疼 在update中调用SceneViewCameraFace2Target函数,编辑器的OnInspec ...

  9. [Swift通天遁地]九、拔剑吧-(1)实现在程序中跳转到微信、App Store、地图

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. Vue导航守卫beforeRouteEnter,beforeRouteUpdate,beforeRouteLeave详解

    Vue导航守卫以我自己的理解就是监听页面进入,修改,和离开的功能.每个守卫接受三个参数 to: Route: 即将要进入的目标路由对象 from: Route: 当前导航正要离开的路由 next: F ...