-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest-path-dijkstra.c
227 lines (181 loc) · 5.35 KB
/
shortest-path-dijkstra.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Problem: Shortest Path between 2 nodes on a directed graph with non-negative weights
Description
Given a directed graph G = (V,E) in which V = {1,2,...,n) is the set of nodes. Each arc (u,v) has a non-negative weight w(u,v). Given two nodes s and t of G. Find the shortest path from s to t on G.
Input
Line 1: contains two integers n and m which are the number of nodes and the number of arcs of G (1 <= n <= 100000)
Line i + 1(i = 1,2,...,m): contains 3 integers u, v, w in which w is the weight of arc(u,v) (0 <= w <= 100000)
Line m+2: contains two integers s and t
Output
Write the weight of the shortest path found or write -1 if no path from s to t was found
Example
Input
5 7
2 5 87
1 2 97
4 5 78
3 1 72
1 4 19
2 3 63
5 1 18
1 5
Output
97
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_NODES 100000
#define INF INT_MAX
struct Edge {
int dest;
int weight;
struct Edge* next;
};
struct Node {
int id;
int distance;
struct Edge* edges;
};
struct Graph {
struct Node nodes[MAX_NODES];
};
struct MinHeapNode {
int v;
int dist;
};
struct MinHeap {
int size;
int capacity;
int* pos;
struct MinHeapNode** array;
};
struct MinHeap* createMinHeap(int capacity)
{
struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->pos = (int*)malloc(capacity * sizeof(int));
minHeap->array = (struct MinHeapNode**)malloc(capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
struct MinHeapNode* newMinHeapNode(int v, int dist)
{
struct MinHeapNode* minHeapNode = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
minHeapNode->v = v;
minHeapNode->dist = dist;
return minHeapNode;
}
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest, left, right;
smallest = idx;
left = 2 * idx + 1;
right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]->dist < minHeap->array[smallest]->dist)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->dist < minHeap->array[smallest]->dist)
smallest = right;
if (smallest != idx) {
struct MinHeapNode* smallestNode = minHeap->array[smallest];
struct MinHeapNode* idxNode = minHeap->array[idx];
minHeap->pos[smallestNode->v] = idx;
minHeap->pos[idxNode->v] = smallest;
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
int isEmpty(struct MinHeap* minHeap)
{
return minHeap->size == 0;
}
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
if (isEmpty(minHeap))
return NULL;
struct MinHeapNode* root = minHeap->array[0];
struct MinHeapNode* lastNode = minHeap->array[minHeap->size - 1];
minHeap->array[0] = lastNode;
minHeap->pos[root->v] = minHeap->size - 1;
minHeap->pos[lastNode->v] = 0;
--minHeap->size;
minHeapify(minHeap, 0);
return root;
}
void decreaseKey(struct MinHeap* minHeap, int v, int dist)
{
int i = minHeap->pos[v];
minHeap->array[i]->dist = dist;
while (i && minHeap->array[i]->dist < minHeap->array[(i - 1) / 2]->dist) {
minHeap->pos[minHeap->array[i]->v] = (i - 1) / 2;
minHeap->pos[minHeap->array[(i - 1) / 2]->v] = i;
swapMinHeapNode(&minHeap->array[i], &minHeap->array[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void printShortestPath(struct Graph* graph, int src, int dest)
{
int V = MAX_NODES;
int* dist = (int*)malloc(V * sizeof(int));
int* parent = (int*)malloc(V * sizeof(int));
struct MinHeap* minHeap = createMinHeap(V);
for (int v = 0; v < V; ++v) {
dist[v] = INF;
parent[v] = -1;
minHeap->array[v] = newMinHeapNode(v, dist[v]);
minHeap->pos[v] = v;
}
minHeap->array[src] = newMinHeapNode(src, dist[src]);
minHeap->pos[src] = src;
dist[src] = 0;
decreaseKey(minHeap, src, dist[src]);
minHeap->size = V;
while (!isEmpty(minHeap)) {
struct MinHeapNode* minHeapNode = extractMin(minHeap);
int u = minHeapNode->v;
struct Edge* temp = graph->nodes[u].edges;
while (temp != NULL) {
int v = temp->dest;
if (dist[u] != INF && (dist[u] + temp->weight) < dist[v]) {
dist[v] = dist[u] + temp->weight;
parent[v] = u;
decreaseKey(minHeap, v, dist[v]);
}
temp = temp->next;
}
}
if (dist[dest] == INF)
printf("-1\n");
else
printf("%d\n", dist[dest]);
}
int main()
{
int n, m;
scanf("%d %d", &n, &m);
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
for (int i = 1; i <= n; i++) {
graph->nodes[i].id = i;
graph->nodes[i].distance = INF;
graph->nodes[i].edges = NULL;
}
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
struct Edge* edge = (struct Edge*)malloc(sizeof(struct Edge));
edge->dest = v;
edge->weight = w;
edge->next = graph->nodes[u].edges;
graph->nodes[u].edges = edge;
}
int s, t;
scanf("%d %d", &s, &t);
printShortestPath(graph, s, t);
return 0;
}