-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
51 lines (49 loc) · 1.13 KB
/
dijkstra.cpp
File metadata and controls
51 lines (49 loc) · 1.13 KB
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
#include <iostream>
using namespace std;
int dijkstra(int graph[5][5], int start, int end)
{
int distance[5];
bool visited[5];
int count, mindistance, nextnode, i, j;
for (i = 0; i < 5; i++)
{
distance[i] = 999;
visited[i] = false;
}
distance[start] = 0;
for (count = 0; count < 5; count++)
{
mindistance = 999;
for (i = 0; i < 5; i++)
{
if (distance[i] < mindistance && !visited[i])
{
mindistance = distance[i];
nextnode = i;
}
}
visited[nextnode] = true;
for (i = 0; i < 5; i++)
{
if (!visited[i])
{
if (mindistance + graph[nextnode][i] < distance[i])
{
distance[i] = mindistance + graph[nextnode][i];
}
}
}
}
return distance[end];
}
int main()
{
int graph[5][5] = {
{0, 1, 2, 3, 4},
{1, 0, 5, 6, 7},
{2, 5, 0, 8, 9},
{3, 6, 8, 0, 10},
{4, 7, 9, 10, 0}};
cout << dijkstra(graph, 0, 4) << endl;
return 0;
}