-
Notifications
You must be signed in to change notification settings - Fork 0
/
11780.cpp
58 lines (48 loc) · 1.07 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
#include <iostream>
#include <vector>
using namespace std;
#define INF 1e9
int d[100][100];
int v[100][100];
int main(){
ios_base::sync_with_stdio(0);
int n,m;
cin >> n >> m;
for(int i=0 ;i<n;i++)
for(int j=0 ;j<n;j++)
d[i][j] = (i==j ? 0 : INF);
for(int i=0 ;i<m;i++){
int a,b,c;
cin >> a >> b >> c;
if(d[a-1][b-1] > c) d[a-1][b-1] = c,v[a-1][b-1] = b-1;
}
for(int k=0 ;k<n;k++)
for(int i=0 ;i<n;i++)
for(int j=0 ;j<n;j++)
if(d[i][j] > d[i][k] + d[k][j]){
d[i][j] = d[i][k] + d[k][j];
v[i][j] = v[i][k];
}
for(int i=0 ;i<n;i++){
for(int j=0 ;j<n;j++)
if(i==j || d[i][j] == INF) cout << 0 << " ";
else cout << d[i][j] << " ";
cout << '\n';
}
for(int i=0 ;i<n;i++){
for(int j=0; j<n ;j++){
if(i==j || d[i][j] == INF){
cout << 0 << '\n';
continue;
}
vector<int> p(1,i);
for(int k=i;k!=j;){
p.push_back(k=v[k][j]);
}
cout << p.size();
for(int k : p) cout << " " << k+1;
cout << '\n';
}
}
return 0;
}