-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_dsr.cpp
43 lines (36 loc) · 1.18 KB
/
7_dsr.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
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cout << "Enter no. of nodes: ";
cin >> n;
int cost[n][n], next_node[n][n];
cout << "Enter the cost matrix: " << endl;
// If there is no direct path, then the cost is infinity (= INT_MAX)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> cost[i][j];
next_node[i][j] = j;
}
}
// Calculating the updated table as well as the next node table
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (cost[i][k] + cost[k][j] < cost[i][j]) {
cost[i][j] = cost[i][k] + cost[k][j];
next_node[i][j] = next_node[i][k];
}
}
}
}
int x;
cout << "Enter the source vertex: ";
cin >> x;
// Printing the shortest paths from the source vertex
cout << "The shortest paths from vertex " << char(x+65) << " are:" << endl;
for (int i = 0; i < n; i++) {
cout << char(x+65) << " -> " << char(i+65) << ": " << cost[x][i] << endl;
}
return 0;
}