#include<bits/stdc++.h> using namespace std; typedef long long ll; #define fi first #define se second const int N=405; int n; int d[N][N]; bitset<401>b[N][N]; bitset<401>all; bool check(int t){ //cout << "check " << t << endl; for(int u=1; u<=n ;u++){ for(int v=u+1; v<=n ;v++){ bool ok=true; for(int k=1; k<=n ;k++){ auto haha=b[k][t]; if(d[u][k]<=t) haha|=b[v][t-d[u][k]]; if(d[v][k]<=t) haha|=b[u][t-d[v][k]]; if(haha!=all){ ok=false; break; } } if(ok) return true; } } return false; } void solve(){ cin >> n; all.reset(); for(int i=1; i<=n ;i++){ for(int j=0; j<=n ;j++){ d[i][j]=1e9; b[i][j].reset(); } all.set(i,1); d[i][i]=0; } for(int i=1; i<=n ;i++){ for(int j=1; j<=n ;j++){ char c;cin >> c; if(c=='1') d[i][j]=1; } } for(int k=1; k<=n ;k++) for(int i=1; i<=n ;i++) for(int j=1; j<=n ;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); for(int i=1; i<=n ;i++){ for(int j=0; j<=n ;j++){ for(int k=1; k<=n ;k++){ if(d[i][k]<=j) b[i][j].set(k,1); } } } int l=0,r=n-1; while(l!=r){ int mid=(l+r)/2; if(check(mid)) r=mid; else l=mid+1; } cout << l << '\n'; } int main(){ ios::sync_with_stdio(false);cin.tie(0); int t;cin >> t;while(t--) 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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; #define fi first #define se second const int N=405; int n; int d[N][N]; bitset<401>b[N][N]; bitset<401>all; bool check(int t){ //cout << "check " << t << endl; for(int u=1; u<=n ;u++){ for(int v=u+1; v<=n ;v++){ bool ok=true; for(int k=1; k<=n ;k++){ auto haha=b[k][t]; if(d[u][k]<=t) haha|=b[v][t-d[u][k]]; if(d[v][k]<=t) haha|=b[u][t-d[v][k]]; if(haha!=all){ ok=false; break; } } if(ok) return true; } } return false; } void solve(){ cin >> n; all.reset(); for(int i=1; i<=n ;i++){ for(int j=0; j<=n ;j++){ d[i][j]=1e9; b[i][j].reset(); } all.set(i,1); d[i][i]=0; } for(int i=1; i<=n ;i++){ for(int j=1; j<=n ;j++){ char c;cin >> c; if(c=='1') d[i][j]=1; } } for(int k=1; k<=n ;k++) for(int i=1; i<=n ;i++) for(int j=1; j<=n ;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); for(int i=1; i<=n ;i++){ for(int j=0; j<=n ;j++){ for(int k=1; k<=n ;k++){ if(d[i][k]<=j) b[i][j].set(k,1); } } } int l=0,r=n-1; while(l!=r){ int mid=(l+r)/2; if(check(mid)) r=mid; else l=mid+1; } cout << l << '\n'; } int main(){ ios::sync_with_stdio(false);cin.tie(0); int t;cin >> t;while(t--) solve(); } |