-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.cpp
More file actions
58 lines (48 loc) · 1.4 KB
/
Solution.cpp
File metadata and controls
58 lines (48 loc) · 1.4 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
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int findMinDistance(vector<int>& dist, vector<bool>& visited, int n) {
int min = INT_MAX, minIndex = -1;
for (int v = 0; v < n; v++) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}
return minIndex;
}
void dijkstra(vector<vector<int>>& graph, int n, int start) {
vector<int> dist(n, INT_MAX);
vector<bool> visited(n, false);
dist[start] = 0;
for (int count = 0; count < n - 1; count++) {
int u = findMinDistance(dist, visited, n);
visited[u] = true;
for (int v = 0; v < n; v++) {
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
cout << "Vertex\tDistance from Source" << endl;
for (int i = 0; i < n; i++) {
cout << i << "\t" << dist[i] << endl;
}
}
int main() {
int n, start;
cout << "Enter the number of vertices: ";
cin >> n;
vector<vector<int>> graph(n, vector<int>(n));
cout << "Enter the adjacency matrix:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> graph[i][j];
}
}
cout << "Enter the starting vertex: ";
cin >> start;
dijkstra(graph, n, start);
return 0;
}