#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <climits>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
// Read the highway connections
vector<vector<bool>> highways(n, vector<bool>(n, false));
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < n; j++) {
highways[i][j] = (s[j] == '1');
}
}
// Initialize shortest path distances (Floyd-Warshall)
vector<vector<int>> dist(n, vector<int>(n, INT_MAX / 2));
for (int i = 0; i < n; i++) {
dist[i][i] = 0;
for (int j = 0; j < n; j++) {
if (highways[i][j]) {
dist[i][j] = 1;
}
}
}
// Floyd-Warshall algorithm to compute shortest paths
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]);
}
}
}
// Current maximum distance without teleport
int maxDistance = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
maxDistance = max(maxDistance, dist[i][j]);
}
}
// Try all possible teleport placements
int bestResult = maxDistance;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Create temporary distance matrix with teleport between i and j
vector<vector<int>> newDist = dist;
// Update distances considering the teleport
for (int a = 0; a < n; a++) {
for (int b = 0; b < n; b++) {
// Using teleport from i to j or j to i
newDist[a][b] = min(newDist[a][b],
min(dist[a][i] + dist[j][b],
dist[a][j] + dist[i][b]));
}
}
// Calculate maximum distance with this teleport
int currentMax = 0;
for (int a = 0; a < n; a++) {
for (int b = 0; b < n; b++) {
currentMax = max(currentMax, newDist[a][b]);
}
}
// Update best result
bestResult = min(bestResult, currentMax);
}
}
cout << bestResult << endl;
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <string> #include <climits> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; // Read the highway connections vector<vector<bool>> highways(n, vector<bool>(n, false)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < n; j++) { highways[i][j] = (s[j] == '1'); } } // Initialize shortest path distances (Floyd-Warshall) vector<vector<int>> dist(n, vector<int>(n, INT_MAX / 2)); for (int i = 0; i < n; i++) { dist[i][i] = 0; for (int j = 0; j < n; j++) { if (highways[i][j]) { dist[i][j] = 1; } } } // Floyd-Warshall algorithm to compute shortest paths 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]); } } } // Current maximum distance without teleport int maxDistance = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { maxDistance = max(maxDistance, dist[i][j]); } } // Try all possible teleport placements int bestResult = maxDistance; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Create temporary distance matrix with teleport between i and j vector<vector<int>> newDist = dist; // Update distances considering the teleport for (int a = 0; a < n; a++) { for (int b = 0; b < n; b++) { // Using teleport from i to j or j to i newDist[a][b] = min(newDist[a][b], min(dist[a][i] + dist[j][b], dist[a][j] + dist[i][b])); } } // Calculate maximum distance with this teleport int currentMax = 0; for (int a = 0; a < n; a++) { for (int b = 0; b < n; b++) { currentMax = max(currentMax, newDist[a][b]); } } // Update best result bestResult = min(bestResult, currentMax); } } cout << bestResult << endl; } return 0; } |
English