-
Notifications
You must be signed in to change notification settings - Fork 1
/
125.cc
126 lines (109 loc) · 3 KB
/
125.cc
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <algorithm>
#include <iostream>
enum {
#ifdef ONLINE_JUDGE
DEBUG = 0,
#else
DEBUG = 1,
#endif
};
enum { MAX_NUM_INTERSECTIONS = 30, };
class Matrix {
public:
Matrix();
explicit Matrix(const Matrix& other);
void Reset();
int at(int i, int j) const;
int& at(int i, int j);
private:
int matrix_[(MAX_NUM_INTERSECTIONS + 1)][(MAX_NUM_INTERSECTIONS + 1)];
};
void PrintMatrix(const Matrix& matrix, int size, std::ostream& out);
int main() {
for (int city = 0, num_streets; std::cin >> num_streets; city++) {
if (num_streets == 0) {
std::cout << "matrix for city " << city << std::endl;
continue;
}
Matrix matrix;
int num_intersections = 0;
while (num_streets-- > 0) {
int i, j;
std::cin >> i >> j;
matrix.at(i, j)++;
num_intersections = std::max(num_intersections, i + 1);
num_intersections = std::max(num_intersections, j + 1);
}
Matrix m0_(matrix), m1_, m_paths(matrix);
Matrix* m0 = &m0_;
Matrix* m1 = &m1_;
for (int n = 0; n < num_intersections; n++) {
for (int k = 0; k < num_intersections; k++) {
for (int i = 0; i < num_intersections; i++) {
for (int j = 0; j < num_intersections; j++) {
m1->at(i, j) += m0->at(i, k) * matrix.at(k, j);
}
}
}
if (DEBUG) {
std::cerr << "m1" << std::endl;
PrintMatrix(*m1, num_intersections, std::cerr);
}
for (int i = 0; i < num_intersections; i++) {
for (int j = 0; j < num_intersections; j++) {
m_paths.at(i, j) += m1->at(i, j);
}
}
std::swap(m0, m1);
m1->Reset();
}
for (int k = 0; k < num_intersections; k++) {
if (!m_paths.at(k, k)) {
continue;
}
for (int i = 0; i < num_intersections; i++) {
for (int j = 0; j < num_intersections; j++) {
if (m_paths.at(i, k) && m_paths.at(k, j)) {
m_paths.at(i, j) = -1;
}
}
}
}
std::cout << "matrix for city " << city << std::endl;
for (int i = 0; i < num_intersections; i++) {
for (int j = 0; j < num_intersections; j++) {
if (j > 0) {
std::cout << ' ';
}
std::cout << m_paths.at(i, j);
}
std::cout << std::endl;
}
}
return 0;
}
void PrintMatrix(const Matrix& matrix, int size, std::ostream& out) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
out << ' ' << matrix.at(i, j);
}
out << std::endl;
}
}
Matrix::Matrix() { Reset(); }
Matrix::Matrix(const Matrix& other) {
for (int i = 0; i < MAX_NUM_INTERSECTIONS + 1; i++) {
for (int j = 0; j < MAX_NUM_INTERSECTIONS + 1; j++) {
matrix_[i][j] = other.matrix_[i][j];
}
}
}
void Matrix::Reset() {
for (int i = 0; i < MAX_NUM_INTERSECTIONS + 1; i++) {
for (int j = 0; j < MAX_NUM_INTERSECTIONS + 1; j++) {
matrix_[i][j] = 0;
}
}
}
int Matrix::at(int i, int j) const { return matrix_[i][j]; }
int& Matrix::at(int i, int j) { return matrix_[i][j]; }