-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloyd.cpp
More file actions
48 lines (44 loc) · 861 Bytes
/
Copy pathFloyd.cpp
File metadata and controls
48 lines (44 loc) · 861 Bytes
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
// Baekjoon 11404
// Floyd-Warshall Algorithm
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int INF = 100001;
int n, m, a, b, c;
int doshi[101][101];
int main() {
cin >> n;
cin >> m;
for (int i{ 0 }; i < n+1; i++) {
for (int j{ 0 }; j < n+1; j++) {
doshi[i][j] = INF;
}
}
for (int i{ 0 }; i < m; i++) {
cin >> a >> b >> c;
if (doshi[a][b] < INF) {
if (doshi[a][b] <= c)
continue;
}
doshi[a][b] = c;
}
for (int k{ 1 }; k < n+1; k++) {
for (int i{ 1 }; i < n + 1; i++) {
for (int j{ 1 }; j < n+1; j++) {
doshi[i][j] = min(doshi[i][j], doshi[i][k] + doshi[k][j]);
}
}
}
for (int i{ 1 }; i < n+1; i++) {
for (int j{ 1 }; j < n+1; j++) {
if (doshi[i][j] >= INF || i == j) {
cout << 0 << " ";
}
else {
cout << doshi[i][j] << " ";
}
}
cout << endl;
}
}