自己对Dijstra算法的理解是:

  1. 首先输入保存点,边的权值(注意无向图和有向图在保存时的区别)。
  2. 将表示从起点st到顶点 i 的距离的d[ i ]数组的每一个值初始化为INF,令d[st] = 0。
  3. 遍历d[ ]数组的下标 i (即顶点 i)这个操作是通过优先队列来实现的,然后遍历以顶点 i 为起点的边,更新d[ i ]的最小值。
  4. 最后直接访问d[en],即可得到最短距离。

通过模板题来熟悉一下这个算法吧,最短路之HDU2544

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin) using namespace std;
typedef long long ll;
typedef pair<int,int> P;//first是距离,second是点的编号
const int maxn = ;
int d[maxn];//数组d[i]表示从起点s到顶点 i 的最短距离
int n,m;
struct edge
{
edge(int t,int c):to(t),cost(c){}
int to;//表示这条边的终点
int cost;//该边的权重
};
vector<edge> G[maxn];//储存以下标i为起点的边
priority_queue<P,vector<P>,greater<P> > que;//遍历d[]数组的下标,更新最小值 void init()
{
for(int i = ; i < maxn; i++)
G[i].clear();
for(int i = ; i < m; i++)
{
int st,en,c;
scanf("%d%d%d",&st,&en,&c);
G[st].push_back(edge(en,c));//这是个无向图注意储存的方式
G[en].push_back(edge(st,c));
}
} int main()
{
//FRE();
while(scanf("%d%d",&n,&m) && n+m)
{
init();
for(int i = ; i < maxn; i++)
d[i]= INF;
d[] = ;//起点到起点本身的距离为0
que.push(P(, ));
while(!que.empty())
{
P p = que.top();
que.pop();
int v = p.second;
if(d[v] < p.first) continue;
for(int i = ; i < G[v].size(); i++)
{
edge e = G[v][i];
if(d[e.to] > d[v] + e.cost)
{
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to],e.to));
}
}
}
printf("%d\n",d[n]);
}
return ;
}

另外还有一个用二维数组的写法:

 #include <iostream>
#include <cstring>
#include <cstdio>
#define INF 0x3f3f3f3f using namespace std;
const int maxn = ; int e[maxn][maxn];
int dis[maxn],vis[maxn];
int n,m; void init()
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
e[i][j] = INF;
}
}
int a,b,c;
for(int i = ; i < m; i++)
{
scanf("%d%d%d",&a,&b,&c);
e[a][b] = c;
e[b][a] = c;
}
for(int i = ; i <= n; i++)//算出1到各个点的距离
{
dis[i] = e[][i];
vis[i] = ;
}
vis[] = ;
} void Dij()
{
for(int i = ; i <= n; i++)
{
int mmin = INF;
int u;
for(int j = ; j <= n; j++)
{
if(!vis[j] && dis[j] < mmin)
{
mmin = dis[j];
u = j;
}
}
vis[u] = ;
for(int j = ; j <= n; j++)
{
if(!vis[j] && dis[j] > e[u][j] + dis[u])
{
dis[j] = e[u][j] + dis[u];
}
}
}
} int main()
{
while(scanf("%d%d",&n,&m) && (m + n))
{
init();
Dij();
printf("%d\n",dis[n]);
}
return ;
}

Dijkstra算法模板的更多相关文章

  1. 最短路径---dijkstra算法模板

    dijkstra算法模板 http://acm.hdu.edu.cn/showproblem.php?pid=1874 #include<stdio.h> #include<stri ...

  2. dijkstra算法模板及其用法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  3. 【hdu 2544最短路】【Dijkstra算法模板题】

    Dijkstra算法 分析 Dijkstra算法适用于边权为正的情况.它可用于计算正权图上的单源最短路( Single-Source Shortest Paths, SSSP) , 即从单个源点出发, ...

  4. 图的最短路径算法Dijkstra算法模板

    Dijkstra算法:伪代码 //G为图,一般设为全局变量,数组d[u]为原点到达个点的额最短路径, s为起点 Dijkstra(G, d[u], s){ 初始化: for (循环n次){ u = 是 ...

  5. dijkstra算法 模板

    算法理解见: https://www.bilibili.com/video/av18586085/?p=83 模板: #define INF 1000000000 int N; int dist[10 ...

  6. hdu-2544-最短路(dijkstra算法模板)

    题目链接 题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa Dijkstra链接 Floyd链接 Bellman-Ford链接 SPFA ...

  7. 最短路径Dijkstra算法模板题---洛谷P3371 【模板】单源最短路径(弱化版)

    题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入格式 第一行 ...

  8. 迪杰斯特拉/dijkstra 算法模板(具体凝视)

    #include <iostream> #include <malloc.h> #include <cstring> #include <stack> ...

  9. 最短路Dijkstra算法模板

    // // dijkstra妯℃澘.cpp // algorithm // // Created by david.xu on 2018/8/6. // Copyright 漏 2018骞?david ...

随机推荐

  1. LLDB 常用命令

    dump: memory read --force --outfile [文件名] --binary [start_address] [end_address] 查找函数地址: 对于有调试符号的这样使 ...

  2. football statistics

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

  3. Cadence——每次启动软件弹出找不到license文件的提示窗口

    1. 摘要 按照Cadence16.60,每次启动该软件,总弹出提示窗口,内如大致为:Orcad Capture license was not found.... 2. 解决方法 参考此链接:htt ...

  4. ZOJ 1860:Dog & Gopher

    Dog & Gopher Time Limit: 2 Seconds      Memory Limit: 65536 KB A large field has a dog and a gop ...

  5. raid5什么意思?如何做raid5?raid5 几块硬盘?

    raid5什么意思?如何做raid5?raid5 几块硬盘? 分类: 项目管理2012-09-28 00:38 5326人阅读 评论(0) 收藏 举报 一.raid什么意思?RAID是“Redunda ...

  6. YES,IT IS

  7. 【转载】Sybase数据库服务器端安装

    sybase数据库的安装分为服务器端和客户端,本文先介绍一下服务器端的安装. 1.和其他程序一样,双击setup.exe.   2.出现欢迎界面,直接点击next即可.   3.下面选择相应国家的协议 ...

  8. Filter,Interceptor和Aspect

    过滤器使用的主要是反射 :拦截器使用的主要是回调 :AOP使用的主要是动态代理. 一个请求过来 ,先进行过滤器处理,看程序是否受理该请求.过滤器放过后, 程序中的拦截器进行处理,处理完后进入被AOP动 ...

  9. [Swift通天遁地]三、手势与图表-(3)通过捏合手势放大和缩小图像视图

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

  10. SS配置,Brook是什么?,Brook如何配置(Android篇)

    很长时间没有更新了,今天给大家分享一下什么是Brook,和SS有什么区别?写的不好,请勿见外,大佬绕过. Brook简单介绍 Brook 是一个高效的 Socks5 代理软件,官方支持Windows. ...