Dijkstra算法

Dijkstra算法的浅要理解

思路

初始化距离数组 dist[N],起点 dist[1] 为 0,其余所有点为0x3f3f3f3f 使用一个集合来存储已经确定最短路的点, 每次先找到一个不在集合中的 dist 最短的点 t,将其放入集合并用其更新其余所有点的 dist

dist[j] = min(dist[j], dist[t] + g[t][j]);

即可求出起点到点n的最短路

代码实现

朴素版 O(n^2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstring>

using namespace std;

const int N = 510;

int g[N][N], dist[N];

bool st[N];

int n, m;

int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for (int i = 0; i < n; i++)
{
int t = -1;
for (int j = 1; j <= n; j++)
if (!st[j] && (t == -1 dist[t] > dist[j]))
t = j;
st[t] = true;
for (int j = 1; j <= n; j++)
{
if (dist[j] > dist[t] + g[t][j])
dist[j] = dist[t] + g[t][j];
}
}
if (dist[n] == 0x3f3f3f3f)
return -1;
else
return dist[n];
}

int main()
{
memset(g, 0x3f, sizeof g);
scanf("%d%d", &n, &m);
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a][b] = min(g[a][b], c);
}
printf("%d", dijkstra());
return 0;
}

堆优化版 O(mlogn)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

typedef pair<int, int> PII;

const int N = 1e5 + 10;

int h[N], e[N], ne[N], idx, w[N];

int n, m;

int dist[N];

bool st[N];

void add(int x, int y, int z)
{
e[++idx] = y;
ne[idx] = h[x];
h[x] = idx;
w[idx] = z;
}

int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, 1});
dist[1] = 0;
while (heap.size())
{
auto t = heap.top();
heap.pop();
int distance = t.first, ver = t.second;
if (st[ver])
continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > distance + w[i])
{
dist[j] = distance + w[i];
heap.push({dist[j], j});
}
}
}
if (dist[n] == 0x3f3f3f3f)
return -1;
else
return dist[n];
}

int main()
{
memset(h, -1, sizeof h);
scanf("%d%d", &n, &m);
while (m--)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
}
cout << dijkstra() << endl;
return 0;
}