#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; void relax(vector<vector<int>>& fw) { for (int k = 0; k < fw.size(); ++k) for (int i = 0; i < fw.size(); ++i) for (int j = 0; j < fw.size(); ++j) if (fw[i][j] > fw[i][k] + fw[k][j]) fw[i][j] = fw[i][k] + fw[k][j]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<vector<int>> matrix(n, vector<int>(n, 0)); for (int i = 0; i < n; ++i) { string s; cin >> s; for (int j = 0; j < n; ++j) if (i != j) { if (s[j] == '1') { matrix[i][j] = 1; } else { matrix[i][j] = 1000000000; } } } relax(matrix); vector<pair<int, int>> most_expensive; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) most_expensive.emplace_back(i, j); most_expensive.emplace_back(0, 0); sort(most_expensive.begin(), most_expensive.end(), [&matrix](pair<int, int> x, pair<int, int> y) { return matrix[y.first][y.second] < matrix[x.first][x.second]; }); int worst = matrix[most_expensive[0].first][most_expensive[0].second]; int best = worst; for (auto [x, y] : most_expensive) { if (matrix[x][y] < worst - best) break; int sol_here = 0; for (auto [i, j] : most_expensive) { if (matrix[i][j] <= sol_here) { if (best > sol_here) best = sol_here; break; } int new_distance = min({matrix[i][j], matrix[i][x] + matrix[y][j], matrix[i][y] + matrix[x][j]}); if (new_distance >= best) break; if (new_distance > sol_here) sol_here = new_distance; } } cout << best << endl; } }
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 | #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; void relax(vector<vector<int>>& fw) { for (int k = 0; k < fw.size(); ++k) for (int i = 0; i < fw.size(); ++i) for (int j = 0; j < fw.size(); ++j) if (fw[i][j] > fw[i][k] + fw[k][j]) fw[i][j] = fw[i][k] + fw[k][j]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<vector<int>> matrix(n, vector<int>(n, 0)); for (int i = 0; i < n; ++i) { string s; cin >> s; for (int j = 0; j < n; ++j) if (i != j) { if (s[j] == '1') { matrix[i][j] = 1; } else { matrix[i][j] = 1000000000; } } } relax(matrix); vector<pair<int, int>> most_expensive; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) most_expensive.emplace_back(i, j); most_expensive.emplace_back(0, 0); sort(most_expensive.begin(), most_expensive.end(), [&matrix](pair<int, int> x, pair<int, int> y) { return matrix[y.first][y.second] < matrix[x.first][x.second]; }); int worst = matrix[most_expensive[0].first][most_expensive[0].second]; int best = worst; for (auto [x, y] : most_expensive) { if (matrix[x][y] < worst - best) break; int sol_here = 0; for (auto [i, j] : most_expensive) { if (matrix[i][j] <= sol_here) { if (best > sol_here) best = sol_here; break; } int new_distance = min({matrix[i][j], matrix[i][x] + matrix[y][j], matrix[i][y] + matrix[x][j]}); if (new_distance >= best) break; if (new_distance > sol_here) sol_here = new_distance; } } cout << best << endl; } } |