#include <iostream> #include <iomanip> #include <string> #include <memory> #include <algorithm> #include <cassert> // Config selector: // 1 == Debug // 0 == Release #if 0 // Debug config #define ASSERT(expr) assert(expr) #define DBG(expr) expr #else // Release config #define ASSERT(expr) do {} while (0) #define DBG(expr) do {} while (0) #endif static constexpr int INF = 1'000'000'000; /* * Implements n*n matrix of integers. */ class Matrix { public: explicit Matrix(int n, int val = 0): m(new int[n * n]), n(n) { fill(val); } void fill(int val) { std::fill(m.get(), m.get() + n * n, val); } int get_n() const { return n; } int & operator()(int row, int col) { ASSERT(row >= 0 && row < n); ASSERT(col >= 0 && col < n); return m[row * n + col]; } int operator()(int row, int col) const { ASSERT(row >= 0 && row < n); ASSERT(col >= 0 && col < n); return m[row * n + col]; } private: std::unique_ptr<int[]> m; // array of size n * n int n; }; std::ostream & operator<<(std::ostream & out, Matrix const & m) { int const n = m.get_n(); for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { std::cout << std::setw(3) << m(row, col) << ' '; } std::cout << '\n'; } return out; } int compute_radius(Matrix const & dist) { int const n = dist.get_n(); int r = INF; for (int row = 0; row < n; ++row) { int row_max_dist = 0; for (int col = 0; col < n; ++col) { row_max_dist = std::max(row_max_dist, dist(row, col)); } r = std::min(r, row_max_dist); } return r; } int compute_max_distance(Matrix const & dist) { int const n = dist.get_n(); int total_max_dist = 0; for (int row = 0; row < n; ++row) { int row_max_dist = 0; for (int col = 0; col < n; ++col) { row_max_dist = std::max(row_max_dist, dist(row, col)); } total_max_dist = std::max(total_max_dist, row_max_dist); } return total_max_dist; } // Computes matrix of distances between all pairs of vertices. Matrix floyd_warshall(Matrix const & edges) { int const n = edges.get_n(); Matrix dist(n, INF); for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { int const weight = edges(row, col); if (weight) dist(row, col) = weight; } } for (int row = 0; row < n; ++row) { dist(row, row) = 0; } for (int k = 0; k < n; ++k) { for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { dist(row, col) = std::min(dist(row, col), // make sure it won't exceed INF std::min(INF, dist(row, k) + dist(k, col))); } } } return dist; } // Returns a new graph, with vertices to_merge1 and to_merge2 merged into one. Matrix compute_teleport_edges(Matrix const & old_edges, int to_merge1, int to_merge2) { if (to_merge1 > to_merge2) std::swap(to_merge1, to_merge2); ASSERT(to_merge1 < to_merge2); int const n = old_edges.get_n(); // The new graph has the following vertices: // v_new in [0; to_merge2-1]: take vertex of idx==v_new from the old graph, // but if v_new==to_merge1 then consider also edges of to_merge2 from the old graph // v_new in [to_merge2; n-2]: take vertex of idx==v_new+1 from the old graph Matrix new_edges(n - 1); for (int v1_new = 0; v1_new < n - 1; ++v1_new) { for (int v2_new = 0; v2_new < n - 1; ++v2_new) { // Consider edge (v1_new, v2_new) in the new graph. int new_edge_weight = 0; if (v1_new != v2_new) { int const v2_old = v2_new < to_merge2 ? v2_new : v2_new + 1; int const v1_old = v1_new < to_merge2 ? v1_new : v1_new + 1; if (v1_new == to_merge1) { new_edge_weight = old_edges(to_merge1, v2_old) || old_edges(to_merge2, v2_old); } else if (v2_new == to_merge1) { new_edge_weight = old_edges(v1_old, to_merge1) || old_edges(v1_old, to_merge2); } else { new_edge_weight = old_edges(v1_old, v2_old); } } new_edges(v1_new, v2_new) = new_edge_weight; } } return new_edges; } void read_and_solve() { int n; std::cin >> n >> std::ws; DBG(std::cout << "=== read_and_solve, n=" << n << " ===\n"); Matrix edges(n); // Read graph. std::string str; for (int row = 0; row < n; ++row) { std::getline(std::cin, str); ASSERT((int)str.size() == n); for (int col = 0; col < n; ++col) { char chr = str[col]; ASSERT(chr == '0' || chr == '1'); if (chr == '1') { ASSERT(row != col); edges(row, col) = 1; } } } DBG(std::cout << "edges:\n" << edges); Matrix const dist = floyd_warshall(edges); DBG(std::cout << "dist:\n" << dist); int const r = compute_radius(dist); DBG(std::cout << "radius: " << r << '\n'); int min_tank_size = INF; int num_v0_considered = 0; for (int row = 0; row < n; ++row) { int row_max_dist = 0; for (int col = 0; col < n; ++col) { row_max_dist = std::max(row_max_dist, dist(row, col)); } if (row_max_dist == r) { int const v0 = row; // Now find v1. int num_v1_considered = 0; for (int col = 0; col < n; ++col) { if (dist(v0, col) == r/2) { int const v1 = col; // Now find v2, but this time consider just one candidate. for (int v2 = 0; v2 < n; ++v2) { if (dist(v1, v2) == r) { // Create graph with v1 and v2 merged. Matrix const edges_with_teleport = compute_teleport_edges(edges, v1, v2); DBG(std::cout << "\n== candidate graph with vertices " << v1 << " and " << v2 << " merged ==\n" << edges_with_teleport); Matrix const dist_with_teleport = floyd_warshall(edges_with_teleport); DBG(std::cout << "candidate graph radius: " << compute_radius(dist_with_teleport) << '\n'); int const candidate_tank_size = compute_max_distance(dist_with_teleport); DBG(std::cout << "candidate tank size: " << candidate_tank_size << '\n'); min_tank_size = std::min(min_tank_size, candidate_tank_size); // We consider just one candidate for v2. break; } } // End of v1 processing. if (++num_v1_considered == 2) { // We consider only 2 candidates for v1. break; } } } // End of v0 processing. if (++num_v0_considered == 2) { // We consider only 2 candidates for v0. break; } } } DBG(std::cout << "\nRESULT: minimal tank size is: "); std::cout << min_tank_size << '\n'; DBG(std::cout << '\n'); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int t; std::cin >> t >> std::ws; for (int i = 0; i < t; ++i) { read_and_solve(); } }
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | #include <iostream> #include <iomanip> #include <string> #include <memory> #include <algorithm> #include <cassert> // Config selector: // 1 == Debug // 0 == Release #if 0 // Debug config #define ASSERT(expr) assert(expr) #define DBG(expr) expr #else // Release config #define ASSERT(expr) do {} while (0) #define DBG(expr) do {} while (0) #endif static constexpr int INF = 1'000'000'000; /* * Implements n*n matrix of integers. */ class Matrix { public: explicit Matrix(int n, int val = 0): m(new int[n * n]), n(n) { fill(val); } void fill(int val) { std::fill(m.get(), m.get() + n * n, val); } int get_n() const { return n; } int & operator()(int row, int col) { ASSERT(row >= 0 && row < n); ASSERT(col >= 0 && col < n); return m[row * n + col]; } int operator()(int row, int col) const { ASSERT(row >= 0 && row < n); ASSERT(col >= 0 && col < n); return m[row * n + col]; } private: std::unique_ptr<int[]> m; // array of size n * n int n; }; std::ostream & operator<<(std::ostream & out, Matrix const & m) { int const n = m.get_n(); for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { std::cout << std::setw(3) << m(row, col) << ' '; } std::cout << '\n'; } return out; } int compute_radius(Matrix const & dist) { int const n = dist.get_n(); int r = INF; for (int row = 0; row < n; ++row) { int row_max_dist = 0; for (int col = 0; col < n; ++col) { row_max_dist = std::max(row_max_dist, dist(row, col)); } r = std::min(r, row_max_dist); } return r; } int compute_max_distance(Matrix const & dist) { int const n = dist.get_n(); int total_max_dist = 0; for (int row = 0; row < n; ++row) { int row_max_dist = 0; for (int col = 0; col < n; ++col) { row_max_dist = std::max(row_max_dist, dist(row, col)); } total_max_dist = std::max(total_max_dist, row_max_dist); } return total_max_dist; } // Computes matrix of distances between all pairs of vertices. Matrix floyd_warshall(Matrix const & edges) { int const n = edges.get_n(); Matrix dist(n, INF); for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { int const weight = edges(row, col); if (weight) dist(row, col) = weight; } } for (int row = 0; row < n; ++row) { dist(row, row) = 0; } for (int k = 0; k < n; ++k) { for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { dist(row, col) = std::min(dist(row, col), // make sure it won't exceed INF std::min(INF, dist(row, k) + dist(k, col))); } } } return dist; } // Returns a new graph, with vertices to_merge1 and to_merge2 merged into one. Matrix compute_teleport_edges(Matrix const & old_edges, int to_merge1, int to_merge2) { if (to_merge1 > to_merge2) std::swap(to_merge1, to_merge2); ASSERT(to_merge1 < to_merge2); int const n = old_edges.get_n(); // The new graph has the following vertices: // v_new in [0; to_merge2-1]: take vertex of idx==v_new from the old graph, // but if v_new==to_merge1 then consider also edges of to_merge2 from the old graph // v_new in [to_merge2; n-2]: take vertex of idx==v_new+1 from the old graph Matrix new_edges(n - 1); for (int v1_new = 0; v1_new < n - 1; ++v1_new) { for (int v2_new = 0; v2_new < n - 1; ++v2_new) { // Consider edge (v1_new, v2_new) in the new graph. int new_edge_weight = 0; if (v1_new != v2_new) { int const v2_old = v2_new < to_merge2 ? v2_new : v2_new + 1; int const v1_old = v1_new < to_merge2 ? v1_new : v1_new + 1; if (v1_new == to_merge1) { new_edge_weight = old_edges(to_merge1, v2_old) || old_edges(to_merge2, v2_old); } else if (v2_new == to_merge1) { new_edge_weight = old_edges(v1_old, to_merge1) || old_edges(v1_old, to_merge2); } else { new_edge_weight = old_edges(v1_old, v2_old); } } new_edges(v1_new, v2_new) = new_edge_weight; } } return new_edges; } void read_and_solve() { int n; std::cin >> n >> std::ws; DBG(std::cout << "=== read_and_solve, n=" << n << " ===\n"); Matrix edges(n); // Read graph. std::string str; for (int row = 0; row < n; ++row) { std::getline(std::cin, str); ASSERT((int)str.size() == n); for (int col = 0; col < n; ++col) { char chr = str[col]; ASSERT(chr == '0' || chr == '1'); if (chr == '1') { ASSERT(row != col); edges(row, col) = 1; } } } DBG(std::cout << "edges:\n" << edges); Matrix const dist = floyd_warshall(edges); DBG(std::cout << "dist:\n" << dist); int const r = compute_radius(dist); DBG(std::cout << "radius: " << r << '\n'); int min_tank_size = INF; int num_v0_considered = 0; for (int row = 0; row < n; ++row) { int row_max_dist = 0; for (int col = 0; col < n; ++col) { row_max_dist = std::max(row_max_dist, dist(row, col)); } if (row_max_dist == r) { int const v0 = row; // Now find v1. int num_v1_considered = 0; for (int col = 0; col < n; ++col) { if (dist(v0, col) == r/2) { int const v1 = col; // Now find v2, but this time consider just one candidate. for (int v2 = 0; v2 < n; ++v2) { if (dist(v1, v2) == r) { // Create graph with v1 and v2 merged. Matrix const edges_with_teleport = compute_teleport_edges(edges, v1, v2); DBG(std::cout << "\n== candidate graph with vertices " << v1 << " and " << v2 << " merged ==\n" << edges_with_teleport); Matrix const dist_with_teleport = floyd_warshall(edges_with_teleport); DBG(std::cout << "candidate graph radius: " << compute_radius(dist_with_teleport) << '\n'); int const candidate_tank_size = compute_max_distance(dist_with_teleport); DBG(std::cout << "candidate tank size: " << candidate_tank_size << '\n'); min_tank_size = std::min(min_tank_size, candidate_tank_size); // We consider just one candidate for v2. break; } } // End of v1 processing. if (++num_v1_considered == 2) { // We consider only 2 candidates for v1. break; } } } // End of v0 processing. if (++num_v0_considered == 2) { // We consider only 2 candidates for v0. break; } } } DBG(std::cout << "\nRESULT: minimal tank size is: "); std::cout << min_tank_size << '\n'; DBG(std::cout << '\n'); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int t; std::cin >> t >> std::ws; for (int i = 0; i < t; ++i) { read_and_solve(); } } |