-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_DS.cpp
More file actions
79 lines (65 loc) · 1.98 KB
/
Copy pathfinal_DS.cpp
File metadata and controls
79 lines (65 loc) · 1.98 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
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
#include <bits/stdc++.h>
using namespace std;
class DijkstraAdjList
{
public:
void addEdge(list<pair<int, int>> *adjList, int start, int finish, int time);
void dijkstra(list<pair<int, int>> *adjList, int var);
int minDistance(int dist[], bool visited[]);
list<pair<int, int>> *adjList;
int start;
int finish;
int time;
int var;
};
void DijkstraAdjList::addEdge(list<pair<int, int>> *adjList, int start, int finish, int time)
{
adjList[start].push_back(make_pair(finish, time));
adjList[finish].push_back(make_pair(start, time));
}
void DijkstraAdjList::dijkstra(list<pair<int, int>> *adjList, int var)
{
int dist[5];
bool visited[5] = {};
for (int i = 0; i < 5; i++)
dist[i] = INT_MAX;
dist[var] = 0;
for (int i = 0; i < 4; i++)
{
int start = minDistance(dist, visited);
visited[start] = true;
for (list<pair<int, int>>::iterator it = adjList[start].begin(); it != adjList[start].end(); it++)
if (visited[it->first] == false && dist[start] != INT_MAX && dist[it->first] > dist[start] + it->second)
dist[it->first] = dist[start] + it->second;
}
for (int i = 0; i < 5; i++)
cout << "0 --> " << i << " ---> " << dist[i] << endl;
}
int DijkstraAdjList::minDistance(int dist[], bool visited[])
{
int min = INT_MAX, index = 0;
for (int i = 0; i < 5; i++)
{
if (dist[i] <= min && visited[i] == false)
{
min = dist[i];
index = i;
}
}
return index;
}
int main()
{
list<pair<int, int>> *adjList = new list<pair<int, int>>[5];
DijkstraAdjList dijk;
dijk.addEdge(adjList, 0, 1, 2);
dijk.addEdge(adjList, 0, 2, 4);
dijk.addEdge(adjList, 1, 2, 1);
dijk.addEdge(adjList, 1, 3, 4);
dijk.addEdge(adjList, 1, 4, 5);
dijk.addEdge(adjList, 2, 1, 5);
dijk.addEdge(adjList, 2, 3, 2);
dijk.addEdge(adjList, 2, 4, 3);
dijk.addEdge(adjList, 3, 4, 1);
dijk.dijkstra(adjList, 0);
}