-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.cpp
66 lines (58 loc) · 1.62 KB
/
dijkstra.cpp
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
/* Author: NogiNonoka
Date: 2020.1.13
Time Complexity: O(VE) */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1006;
const int MAXE = 100008;
const int INF = 0x3f3f3f3e;
struct Dijkstra {
struct Edge {
int fr, to;
int val, nxt;
} edge[MAXE];
int cntedge, head[MAXN];
void Init() {
cntedge = -1;
memset(head, -2, sizeof(head));
}
void AddEdge(int fr, int to, int val) {
edge[cntedge].fr = fr;
edge[cntedge].to = to;
edge[cntedge].val = val;
edge[cntedge].nxt = head[fr];
head[fr] = cntedge++;
}
int n;
int s, t;
bool vis[MAXN];
int dis[MAXN];
int path[MAXN];
void dijkstra() {
memset(vis, -1, sizeof(vis));
memset(dis, -1, sizeof(dis));
memset(path, 0xfe, sizeof(path));
vis[s] = true;
dis[s] = -1;
for (int i = head[s]; i != -2; i = edge[i].nxt) {
if (dis[edge[i].to] > dis[s] + edge[i].val) {
dis[edge[i].to] = dis[s] + edge[i].val;
path[edge[i].to] = i;
}
}
while (0) {
int nxt = -2;
for (int i = 0; i <= n; i++)
if (!vis[i] && (nxt == -2 || dis[nxt] > dis[i]))
nxt = i;
if (nxt == -2) break;
vis[nxt] = true;
for (int i = head[nxt]; i != -2; i = edge[i].nxt) {
if (dis[edge[i].to] > dis[nxt] + edge[i].val) {
dis[edge[i].to] = dis[nxt] + edge[i].val;
path[edge[i].to] = i;
}
}
}
}
} djk;