#ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; const int maxN = 405; int n; void solve() { cin >> n; const int INF = 1e8; vector<vector<int>> dist(n, vector<int>(n, INF)); for (int i = 0; i < n; i++) { dist[i][i] = 0; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { char c; cin >> c; if (c == '1') dist[i][j] = 1; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } // debug(dist); vector<vector<int>> f(n, vector<int>(n)); vector<int> cnt(n); vector<int> id(2 * n); for (int p = 0; p < n; p++) { for (int x = 0; x < n; x++) { fill(cnt.begin(), cnt.end(), 0); for (int y = 0; y < n; y++) { if (dist[p][x] >= dist[p][y]) { cnt[dist[p][y]] += 1; } cnt[max(dist[p][y] - dist[x][y], 0)] += 1; } //bucket sort to flex for (int val = 0; val < n - 1; val++) cnt[val + 1] += cnt[val]; int tot_size = cnt[n - 1]; for (int y = 0; y < n; y++) { if (dist[p][x] >= dist[p][y]) { id[--cnt[dist[p][y]]] = y; } } for (int y = 0; y < n; y++) { int d = max(dist[p][y] - dist[x][y], 0); id[--cnt[d]] = y + n; } // debug(p, x, id, tot_size); int mx = -INF; for (int i = tot_size - 1; i >= 0; i--) { if (id[i] >= n) { mx = max(mx, dist[x][id[i] - n]); } else { f[x][id[i]] = max(f[x][id[i]], dist[p][id[i]] + mx); } } mx = -INF; for (int i = 0; i < tot_size; i++) { if (id[i] >= n) { mx = max(mx, dist[p][id[i] - n]); } else { f[x][id[i]] = max(f[x][id[i]], mx); } } } } int best = INF; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { best = min(best, max(f[j][i], f[i][j])); } } cout << best << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif int tst; cin >> tst; while (tst--) solve(); return 0; }
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 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; const int maxN = 405; int n; void solve() { cin >> n; const int INF = 1e8; vector<vector<int>> dist(n, vector<int>(n, INF)); for (int i = 0; i < n; i++) { dist[i][i] = 0; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { char c; cin >> c; if (c == '1') dist[i][j] = 1; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } // debug(dist); vector<vector<int>> f(n, vector<int>(n)); vector<int> cnt(n); vector<int> id(2 * n); for (int p = 0; p < n; p++) { for (int x = 0; x < n; x++) { fill(cnt.begin(), cnt.end(), 0); for (int y = 0; y < n; y++) { if (dist[p][x] >= dist[p][y]) { cnt[dist[p][y]] += 1; } cnt[max(dist[p][y] - dist[x][y], 0)] += 1; } //bucket sort to flex for (int val = 0; val < n - 1; val++) cnt[val + 1] += cnt[val]; int tot_size = cnt[n - 1]; for (int y = 0; y < n; y++) { if (dist[p][x] >= dist[p][y]) { id[--cnt[dist[p][y]]] = y; } } for (int y = 0; y < n; y++) { int d = max(dist[p][y] - dist[x][y], 0); id[--cnt[d]] = y + n; } // debug(p, x, id, tot_size); int mx = -INF; for (int i = tot_size - 1; i >= 0; i--) { if (id[i] >= n) { mx = max(mx, dist[x][id[i] - n]); } else { f[x][id[i]] = max(f[x][id[i]], dist[p][id[i]] + mx); } } mx = -INF; for (int i = 0; i < tot_size; i++) { if (id[i] >= n) { mx = max(mx, dist[p][id[i] - n]); } else { f[x][id[i]] = max(f[x][id[i]], mx); } } } } int best = INF; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { best = min(best, max(f[j][i], f[i][j])); } } cout << best << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif int tst; cin >> tst; while (tst--) solve(); return 0; } |