#include <iostream> #include <vector> #include <climits> #include <algorithm> using namespace std; const int max_cities = 400; const int INF = INT_MAX; vector<vector<int>> adj_list; vector<pair<int, int>> edge_list; void print_adj_matrix(int n, vector<vector<int>>& adj_matrix) { cout << "Macierz sąsiedztwa:\n"; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << adj_matrix[i][j] << " "; } cout << "\n"; } cout << "\n"; } void print_adj_list(int n) { cout << "Lista sąsiedztwa:\n"; for (int i = 0; i < n; i++) { cout << "Miasto " << i + 1 << ": "; for (int neighbor : adj_list[i]) { cout << neighbor << " "; } cout << "\n"; } cout << "\n"; } vector<pair<int, pair<int, int>>> table_of_furthest(vector<vector<int>> &matrix) { int n = matrix.size(); vector<pair<int, pair<int, int>>> distances; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { distances.push_back({matrix[i][j], {i, j}}); } } sort(distances.begin(), distances.end()); return distances; } int find_max_distance(vector<vector<int>> &dist) { int n = dist.size(); int max_distance = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { max_distance = max(max_distance, dist[i][j]); } } return max_distance; } void floyd_warshall_algo(vector<vector<int>> &matrix) { int n = matrix.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (matrix[i][k] != INF && matrix[k][j] != INF) { matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j]); } } } } } void solve(int n,const vector<pair<int, int>>& edges) { // Create floyd-marshall vector<vector<int>> floyd_marshall(n, vector<int>(n, INF)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { floyd_marshall[i][j] = INF; } else { floyd_marshall[i][j] = 0; } } } for (const auto& edge : edges) { int v1 = edge.first; int v2 = edge.second; floyd_marshall[v1][v2] = 1; floyd_marshall[v2][v1] = 1; } vector<vector<int>> floyd_basic = floyd_marshall; for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (floyd_marshall[i][k] != INF && floyd_marshall[k][j] != INF && floyd_marshall[i][j] > floyd_marshall[i][k] + floyd_marshall[k][j]) { floyd_marshall[i][j] = floyd_marshall[i][k] + floyd_marshall[k][j]; } } } } print_adj_matrix(n,floyd_marshall); vector<pair<int, pair<int, int>>> furthest_table = table_of_furthest(floyd_marshall); int prev_max_distance = INF; int max_distance = find_max_distance(floyd_marshall); int i = 0; while (prev_max_distance > max_distance) { prev_max_distance = max_distance; vector<vector<int>> floyd_teleport = floyd_basic; pair<int,int> furthest = furthest_table[i].second; floyd_teleport[furthest.first][furthest.second] = 0; floyd_teleport[furthest.second][furthest.first] = 0; floyd_warshall_algo(floyd_teleport); max_distance = find_max_distance(floyd_teleport); i++; } cout << max_distance << "\n"; } int main() { // ios::sync_with_stdio(false); // cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; int adj_matrix[n][n]; adj_list.assign(n, vector<int>()); // Reset for (int i = 0; i < n; i++) { string row; cin >> row; for (int j = 0; j < n; j++) { adj_matrix[i][j] = row[j] - '0'; if (adj_matrix[i][j] == 1) { adj_list[i].push_back(j); edge_list.push_back({i, j}); } } } solve(n,edge_list); } 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <iostream> #include <vector> #include <climits> #include <algorithm> using namespace std; const int max_cities = 400; const int INF = INT_MAX; vector<vector<int>> adj_list; vector<pair<int, int>> edge_list; void print_adj_matrix(int n, vector<vector<int>>& adj_matrix) { cout << "Macierz sąsiedztwa:\n"; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << adj_matrix[i][j] << " "; } cout << "\n"; } cout << "\n"; } void print_adj_list(int n) { cout << "Lista sąsiedztwa:\n"; for (int i = 0; i < n; i++) { cout << "Miasto " << i + 1 << ": "; for (int neighbor : adj_list[i]) { cout << neighbor << " "; } cout << "\n"; } cout << "\n"; } vector<pair<int, pair<int, int>>> table_of_furthest(vector<vector<int>> &matrix) { int n = matrix.size(); vector<pair<int, pair<int, int>>> distances; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { distances.push_back({matrix[i][j], {i, j}}); } } sort(distances.begin(), distances.end()); return distances; } int find_max_distance(vector<vector<int>> &dist) { int n = dist.size(); int max_distance = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { max_distance = max(max_distance, dist[i][j]); } } return max_distance; } void floyd_warshall_algo(vector<vector<int>> &matrix) { int n = matrix.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (matrix[i][k] != INF && matrix[k][j] != INF) { matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j]); } } } } } void solve(int n,const vector<pair<int, int>>& edges) { // Create floyd-marshall vector<vector<int>> floyd_marshall(n, vector<int>(n, INF)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { floyd_marshall[i][j] = INF; } else { floyd_marshall[i][j] = 0; } } } for (const auto& edge : edges) { int v1 = edge.first; int v2 = edge.second; floyd_marshall[v1][v2] = 1; floyd_marshall[v2][v1] = 1; } vector<vector<int>> floyd_basic = floyd_marshall; for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (floyd_marshall[i][k] != INF && floyd_marshall[k][j] != INF && floyd_marshall[i][j] > floyd_marshall[i][k] + floyd_marshall[k][j]) { floyd_marshall[i][j] = floyd_marshall[i][k] + floyd_marshall[k][j]; } } } } print_adj_matrix(n,floyd_marshall); vector<pair<int, pair<int, int>>> furthest_table = table_of_furthest(floyd_marshall); int prev_max_distance = INF; int max_distance = find_max_distance(floyd_marshall); int i = 0; while (prev_max_distance > max_distance) { prev_max_distance = max_distance; vector<vector<int>> floyd_teleport = floyd_basic; pair<int,int> furthest = furthest_table[i].second; floyd_teleport[furthest.first][furthest.second] = 0; floyd_teleport[furthest.second][furthest.first] = 0; floyd_warshall_algo(floyd_teleport); max_distance = find_max_distance(floyd_teleport); i++; } cout << max_distance << "\n"; } int main() { // ios::sync_with_stdio(false); // cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; int adj_matrix[n][n]; adj_list.assign(n, vector<int>()); // Reset for (int i = 0; i < n; i++) { string row; cin >> row; for (int j = 0; j < n; j++) { adj_matrix[i][j] = row[j] - '0'; if (adj_matrix[i][j] == 1) { adj_list[i].push_back(j); edge_list.push_back({i, j}); } } } solve(n,edge_list); } return 0; } |