#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...) (void)0
#endif
namespace R = ranges;
namespace V = views;
auto ra(auto x, auto y) { return R::iota_view(x, y); }
auto rae(auto x, auto y) { return ra(x, y + 1); }
using i64 = int64_t;
const int oo = 1E9 + 7;
const int nax = 400;
int dist[nax][nax];
void solve() {
int n;
cin >> n;
for (auto i : ra(0, n)) {
for (auto j : ra(0, n)) {
char c;
cin >> c;
if (c == '1') {
dist[i][j] = 1;
} else {
dist[i][j] = oo;
}
}
dist[i][i] = 0;
}
for (auto k : ra(0, n)) {
for (auto i : ra(0, n)) {
for (auto j : ra(0, n)) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int ans = oo;
for (auto i : ra(0, n)) {
for (auto j : ra(i + 1, n)) {
int local = -oo;
for (auto k : ra(0, n)) {
for (auto l : ra(k + 1, n)) {
local = max(local, min({dist[k][l], dist[i][k] + dist[j][l],
dist[j][k] + dist[i][l]}));
}
}
ans = min(ans, local);
}
}
cout << ans << '\n';
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
cin >> t;
for (auto tc_n : ra(0, t)) {
debug(tc_n);
solve();
cout.flush();
}
}
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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "debug.hpp" #else #define debug(...) (void)0 #endif namespace R = ranges; namespace V = views; auto ra(auto x, auto y) { return R::iota_view(x, y); } auto rae(auto x, auto y) { return ra(x, y + 1); } using i64 = int64_t; const int oo = 1E9 + 7; const int nax = 400; int dist[nax][nax]; void solve() { int n; cin >> n; for (auto i : ra(0, n)) { for (auto j : ra(0, n)) { char c; cin >> c; if (c == '1') { dist[i][j] = 1; } else { dist[i][j] = oo; } } dist[i][i] = 0; } for (auto k : ra(0, n)) { for (auto i : ra(0, n)) { for (auto j : ra(0, n)) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } int ans = oo; for (auto i : ra(0, n)) { for (auto j : ra(i + 1, n)) { int local = -oo; for (auto k : ra(0, n)) { for (auto l : ra(k + 1, n)) { local = max(local, min({dist[k][l], dist[i][k] + dist[j][l], dist[j][k] + dist[i][l]})); } } ans = min(ans, local); } } cout << ans << '\n'; } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); int t = 1; cin >> t; for (auto tc_n : ra(0, t)) { debug(tc_n); solve(); cout.flush(); } } |
English