洛谷 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. 使用 StackExchange.Redis 封装属于自己的 RedisHelper

    目录 核心类 ConnectionMultiplexer 字符串(String) 哈希(Hash) 列表(List) 有序集合(sorted set) Key 操作 发布订阅 其他 简介 目前 .NE ...

  2. bzoj1977 [BeiJing2010组队]次小生成树 Tree——严格次小生成树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1977 因为严格,所以要记录到 LCA 的一个次小值: 很快写好,然后改掉一堆错误后终于过了样 ...

  3. LVS上DR和NAT模式的缺陷

    引言 相信一般的小公司用的最多的还是DR和NAT模式,关于DR和NAT模式的原理请看看下图,我们先从lvs的DR和NAT模式特性聊聊一些问题. 问题1.lvs的DR模式和NAT模式核心缺陷有哪些? D ...

  4. 洛谷P1250种树(贪心)

    题目描述 一条街的一边有几座房子.因为环保原因居民想要在路边种些树.路边的地区被分割成块,并被编号成1..N.每个部分为一个单位尺寸大小并最多可种一棵树.每个居民想在门前种些树并指定了三个号码B,E, ...

  5. 03-vue实例生命周期和vue-resource

    vue实例的生命周期 什么是生命周期:从Vue实例创建.运行.到销毁期间,总是伴随着各种各样的事件,这些事件,统称为生命周期! 生命周期钩子:就是生命周期事件的别名而已: 生命周期钩子 = 生命周期函 ...

  6. Horspool和BM算法解析

    最近算法中学到了Horspool,KMP,BM三种算法.接下来给大家做个分享. Horspool算法: 算法思路: 1.分为匹配串,原串 2.从右往左依次匹配: 一旦遇到不匹配的,原串相对于匹配串 移 ...

  7. 题解报告:hdu 1272 小希的迷宫

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem ...

  8. UNIX环境高级编程--2

    UNIX标准及实现 ISO C: 国际标准化组织(International Organization for standardization , ISO)ISO C标准的意图是提供C程序的可移植性, ...

  9. C# 利用反射进行类型转换

    /// <summary> /// 父类转子类 /// </summary> /// <typeparam name="TParent">< ...

  10. Java系列学习(十)-包与权限修饰符

    1.形式参数和返回值的问题 (1)形式参数: A:类名:需要该类的对象 B:抽象类名:需要改类的子类对象 C:接口名:需要该接口的实现对象 (2)返回值类型: A:类名:抽象类名:返回的是该类的对象 ...