forked from aplgsd/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra(Heap-Optimised).cpp
145 lines (124 loc) · 1.96 KB
/
Dijkstra(Heap-Optimised).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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <cstdio>
#define INF 1000000000
#define VERTEX_COUNT 100010
#define EDGE_COUNT 1000010
using namespace std;
struct vertex
{
int first_edge;
int dis, hpos;
}V[VERTEX_COUNT];
struct edge
{
int endp, next;
int w;
}E[EDGE_COUNT];
int ec = 1;
int ivc, iec;
int H[VERTEX_COUNT], pos[VERTEX_COUNT];
int heapsize;
void add_edge(int u, int v, int w)
{
E[ec].next = V[u].first_edge;
V[u].first_edge = ec;
E[ec].endp = v;
E[ec].w = w;
ec++;
}
void initial_single_source(int s)
{
for (int i = 1; i <= ivc; i++)
{
V[i].dis = INF;
}
V[s].dis = 0;
}
void build_heap(int s)
{
heapsize = ivc;
for (int i = 1; i <= ivc; i++)
{
H[i] = i;
pos[i] = i;
}
H[s] = 1;
pos[1] = s;
H[1] = s;
pos[s] = 1;
}
void heap_sink(int i)
{
int lch = i << 1;
int rch = lch + 1;
int smallest = i;
if (lch <= heapsize && V[H[lch]].dis < V[H[smallest]].dis)
{
smallest = lch;
}
if (rch <= heapsize && V[H[rch]].dis < V[H[smallest]].dis)
{
smallest = rch;
}
if (smallest != i)
{
V[H[smallest]].hpos = i;
V[H[i]].hpos = smallest;
int temp = H[i];
H[i] = H[smallest];
H[smallest] = temp;
heap_sink(smallest);
}
}
void heap_float(int i)
{
int p = i >> 1;
while (p > 1 && V[H[i]].dis < V[H[p]].dis)
{
pos[H[i]] = p;
pos[H[p]] = i;
int temp = H[i];
H[i] = H[p];
H[p] = temp;
i = p;
p = i >> 1;
}
}
int extract_min()
{
int res = H[1];
H[1] = H[heapsize--];
pos[H[1]] = 1;
heap_sink(1);
return res;
}
void Dijkstra(int s)
{
initial_single_source(s);
build_heap(s);
while (heapsize > 0)
{
int u = extract_min();
for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next)
{
int newpath = V[u].dis + E[cur].w;
if (newpath < V[E[cur].endp].dis)
{
V[E[cur].endp].dis = newpath;
heap_float(pos[E[cur].endp]);
}
}
}
}
int main()
{
scanf("%d%d", &ivc, &iec);
for (int i = 0; i < iec; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
}
Dijkstra(1);
printf("%d", V[ivc].dis);
return 0;
}