-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal_lab0.cpp
More file actions
60 lines (53 loc) · 1.38 KB
/
Copy pathFinal_lab0.cpp
File metadata and controls
60 lines (53 loc) · 1.38 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
#include <bits/stdc++.h>
using namespace std;
const int infinity = 1e9;
int main(){
int n, e;
cin >> n >> e;
vector<vector<int>> dis(n, vector<int>(n, infinity));
vector<vector<int>> par(n, vector<int>(n, -1));
for(int i = 0; i<n; i++){
dis[i][i] = 0;
par[i][i] = i;
}
while(e--){
int a,b,w;
cin >> a >> b >> w;
dis[a][b] = w;
par[a][b] = a;
}
for(int k = 0; k<n; k++){
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(dis[i][k] < infinity && dis[k][j] < infinity){
if(dis[i][j] > dis[i][k] + dis[k][j]){
dis[i][j] = dis[i][k] + dis[k][j];
par[i][j] = par[k][j];
}
}
}
}
}
// Output distance matrix
cout << "Distance Matrix:" << endl;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(dis[i][j] == infinity){
cout << "Infinite";
}
else{
cout << dis[i][j] << "\t";
}
}
cout << endl;
}
// Output parent matrix
cout << endl << "Parent Matrix:" << endl;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
cout << par[i][j] << "\t";
}
cout << endl;
}
return 0;
}