-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_11780.cpp
More file actions
102 lines (91 loc) · 1.91 KB
/
BOJ_11780.cpp
File metadata and controls
102 lines (91 loc) · 1.91 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//21 09 06 > 21 10 11
#include <iostream>
#include <vector>
#include <algorithm>
#define INF 987654321
using namespace std;
int tracking[101][101];
int bus[101][101];
int n, m, a, b, c;
void Floyd(void)
{
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
for (int k=1; k<=n; k++)
{
if (bus[j][k] > bus[j][i] + bus[i][k])
{
bus[j][k] = bus[j][i] + bus[i][k];
tracking[j][k] = i;
}
}
}
}
}
void Back_tracking(int x, int y, vector<int>&temp)
{
if (x==y || bus[x][y]==INF)
return;
if (tracking[x][y] == x)
{
temp.push_back(x);
return;
}
int z = tracking[x][y];
Back_tracking(x,z, temp);
Back_tracking(z,y, temp);
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
bus[i][j] = INF;
tracking[i][j] = i;
}
bus[i][i] = 0;
}
while (m--)
{
cin >> a>> b>> c;
if (bus[a][b]>c)
bus[a][b] = c;
}
Floyd();
for (int i=1; i<=n ;i++)
{
for (int j=1; j<=n; j++)
{
if (bus[i][j] == INF)
cout << 0 << " ";
else
cout << bus[i][j] << " ";
}
cout<<"\n";
}
for (int i=1; i<=n ;i++)
{
for (int j=1; j<=n; j++)
{
vector<int> temp;
Back_tracking(i, j, temp);
if (temp.empty())
{
cout<<0<<"\n";
continue;
}
cout<<((int)temp.size())+1<<" ";
for (auto iter = temp.begin(); iter!=temp.end(); iter++)
cout<<*iter<<" ";
cout<<j<<" ";
cout<<"\n";
}
}
return 0;
}