-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11780.cpp
89 lines (65 loc) · 1.58 KB
/
11780.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9+10;
//시작 도시와 도착 도시를 연결하는 노선은 하나가 아닐 수 있다.
//0번 index는 사용X
int table[105][105];
int nxt[105][105];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin>>n>>m;
//table 초기 값!
for(int i=1; i<=n; i++)
fill(table[i], table[i] + 105, INF);
int a,b,c;
//입력받으면서 바로 table에 적어주기
for(int i=0; i<m; i++){
cin>>a>>b>>c;
table[a][b] = min(table[a][b], c);
nxt[a][b] = b;
}
for(int i=1; i<=n; i++)
table[i][i] = 0;
//[i][i]를 0으로 채워야 되는 이유!!!
// [2][1] + [1][2] < [2][2] 가 항상 성립되니깐! 값이 바뀌어버림!
for(int i=1; i<=n; i++){
for(int s = 1; s<=n; s++){
for(int t=1; t<=n; t++){
if(table[s][i] + table[i][t] < table[s][t]){
table[s][t] = table[s][i] + table[i][t];
nxt[s][t] = nxt[s][i];
}
}
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(table[i][j] == INF)
cout<<0<<" ";
else cout<<table[i][j]<<" ";
}
cout<<'\n';
}
//nxt 확인하기
for(int s = 1; s<=n; s++){
for(int t = 1; t<=n; t++){
vector<int> path;
int cur = s;
while(cur != t){
path.push_back(cur);
cur = nxt[cur][t];
}
path.push_back(cur);
if(path.size() == 1){
cout<<0<<'\n';
continue;
}
cout<<path.size()<<" ";
for(auto v : path)
cout<<v<<" ";
cout<<'\n';
}
}
}