forked from encrypted-def/basic-algo-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11780.cpp
61 lines (55 loc) · 1.3 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
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/0986ecfcb9c242e3a498148ab5812ad0
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int d[105][105];
int nxt[105][105];
int n, m;
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
fill(d[i], d[i]+1+n, INF);
while(m--){
int a,b,c;
cin >> a >> b >> c;
d[a][b] = min(d[a][b], c);
nxt[a][b] = b;
}
for(int i = 1; i <= n; i++) d[i][i] = 0;
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(d[i][k] + d[k][j] < d[i][j]){
d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
nxt[i][j] = nxt[i][k];
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(d[i][j] == INF) cout << "0 ";
else cout << d[i][j] << ' ';
}
cout << '\n';
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(d[i][j] == 0 || d[i][j] == INF){
cout << "0\n";
continue;
}
vector<int> path;
int st = i;
while(st != j){
path.push_back(st);
st = nxt[st][j];
}
path.push_back(j);
cout << path.size() << ' ';
for(auto x : path) cout << x << ' ';
cout << '\n';
}
}
}