-
Notifications
You must be signed in to change notification settings - Fork 0
/
8_lsr.cpp
60 lines (53 loc) · 1.22 KB
/
8_lsr.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout << "Enter the number of nodes: ";
cin >> n;
int d[n][n];
cout << "Enter the distance matrix: " << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> d[i][j];
}
}
int s;
cout << "Enter the source node: ";
cin >> s;
int dist[n];
bool visited[n] = {false};
for (int i = 0; i < n; i++)
{
dist[i] = 999;
}
dist[s] = 0;
for (int i = 0; i < n; i++)
{
int min = 999, u;
for (int j = 0; j < n; j++)
{
if (visited[j] == false && dist[j] < min)
{
min = dist[j];
u = j;
}
}
visited[u] = true;
for (int v = 0; v < n; v++)
{
if (visited[v] == false && d[u][v] != 0 && dist[u] + d[u][v] < dist[v])
{
dist[v] = dist[u] + d[u][v];
}
}
}
cout << "The shortest path from node " << char(s + 65) << " is:" << endl;
for (int i = 0; i < n; i++)
{
cout << char(s + 65) << " -> " << char(i + 65) << " : " << dist[i] << endl;
}
return 0;
}