洛谷 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. 批量ssh执行命令

    [root@openfire1 script]# cat test.sh  #!/bin/bash   #本地通过ssh执行远程服务器的脚本   for ip in `cat iplist`  do ...

  2. football statistics

    https://www.whoscored.com/Players/24328/Show/Edinson-Cavani

  3. mac 配置hadoop 2.6(单机和伪分布式)

    一.准备工作: 安装jdk >= 1.7: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...

  4. element-ui table 页面加载时,动态渲染后台传过来的数据(springmvc)

    jsp页面 <%@ page contentType="text/html;charset=UTF-8" language="java" %> &l ...

  5. robotframework - 介绍&应用

    一.参考简书链接 :https://www.jianshu.com/p/c3a9d20db4e5 二.介绍 Robot Framework是一个基于Python的,可扩展的关键字驱动的测试自动化框架, ...

  6. Linux搭建tomcat文件服务器

    Linux搭建tomcat文件服务器 Linux下配置Tomcat服务器和Windows下其实差不多,可以去官网下载安装包释放或者在线下载,只是当时下载的windows.zip文件,现在下载.tar. ...

  7. 面试说熟练掌握各种MQ?那你先看看这道题,面试官必问!

    写在前面 我们知道,目前市面上的MQ包括Kafka.RabbitMQ.ZeroMQ.RocketMQ等等. 那么他们之间究竟有什么本质区别,分别适用于什么场景呢? 上述抛出的问题,同样在不少公司的Ja ...

  8. Nginx 配置https请求

    通过阿里云生成指定的https证书文件xxxx.key 和 xxxx.pem文件 在阿里云上申请的https证书的是pem格式,转成cer 先在终端cd到文件目录下 然后 openssl x509 - ...

  9. 324 Wiggle Sort II 摆动排序 II

    给定一个无序的数组nums,将它重新排列成nums[0] < nums[1] > nums[2] < nums[3]...的顺序.例子:(1) 给定nums = [1, 5, 1, ...

  10. Ambari是啥?主要是干啥的?

    简单来说,Ambari是一个拥有集群自动化安装.中心化管理.集群监控.报警功能的一个工具(软件),使得安装集群从几天的时间缩短在几个小时内,运维人员从数十人降低到几人以内,极大的提高集群管理的效率. ...