-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloyd-warshall.cpp
More file actions
54 lines (52 loc) · 1.33 KB
/
floyd-warshall.cpp
File metadata and controls
54 lines (52 loc) · 1.33 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
#include <iostream>
#define MAX 999
using namespace std;
void FW(int** graph, int N, int E);
int main(){
int N, E;
cin >> N >> E;
int** graph = new int* [E];
for (int i = 0; i < E; i ++){
graph[i] = new int [3];
}
for (int i = 0; i < E; i ++){
cin >> graph[i][0] >> graph[i][1] >> graph[i][2];
}
FW(graph, N, E);
}
void FW(int** graph, int N, int E){
int** stp = new int* [N];
for (int i = 0; i < N; i ++){
stp[i] = new int [N];
}
for (int i = 0; i < N; i ++){
for (int j = 0; j < N; j ++){
stp[i][j] = MAX;
}
}
for (int i = 0; i < N; i ++){
stp[i][i] = 0;
}
for (int i = 0; i < E; i ++){
stp[graph[i][0]-1][graph[i][1]-1] = graph[i][2];
}
for (int k = 0; k < N; k ++){
for (int i = 0; i < N; i ++){
for (int j = 0; j < N; j ++){
if (stp[i][j] > stp[i][k] + stp[k][j]){
stp[i][j] = stp[i][k] + stp[k][j];
}
}
}
}
for (int i = 0; i < N; i ++){
for (int j = 0; j < N; j ++){
if (stp[i][j] < MAX){
cout << stp[i][j] << " ";
} else {
cout << "N" << " ";
}
}
cout << endl;
}
}