-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11404.cpp
More file actions
62 lines (58 loc) · 1.19 KB
/
11404.cpp
File metadata and controls
62 lines (58 loc) · 1.19 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
61
62
// 11404. 플로이드
// 2019.05.22
// 플로이드 와샬 알고리즘, 그래프 이론
#include<iostream>
#include<algorithm>
using namespace std;
int d[101][101];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int from, to, cost;
cin >> from >> to >> cost;
if (!d[from][to])
{
d[from][to] = cost;
}
else // 이미 경로가 있는 경우 최소값 선택
{
d[from][to] = min(d[from][to], cost);
}
}
// i를 거쳐가는게 더 빠를 경우 update
for (int i = 1; i <= n; i++)
{
for (int from = 1; from <= n; from++)
{
if (d[from][i] == 0)
{
continue; // 출발지에서 i로 가는 경로가 없을땐 무시
}
for (int to = 1; to <= n; to++)
{
// i에서 to로 가는 비용이 없거나 출발지=도착지 일땐 무시
if (d[i][to] == 0 || from == to)
{
continue;
}
// 아직 비용이 없거나 i를 거치는게 최소 비용일 경우 갱신
if (d[from][to] == 0 || d[from][to] > d[from][i] + d[i][to])
{
d[from][to] = d[from][i] + d[i][to];
}
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << d[i][j] << " ";
}
cout << endl;
}
return 0;
}