#include <iostream> int index(int i,int j){ return i * (i - 1) / 2 + j; } void floydWarshall(int *autostrady,int *distances, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (autostrady[index(i,j)] == 0 && i != j) { distances[index(i,j)] = 401; } else { distances[index(i,j)] = autostrady[index(i,j)]; } } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { int ij = index(i, j); int ik = (i > k) ? index(i, k) : index(k, i); int kj = (k > j) ? index(k, j) : index(j, k); if (distances[ij] > distances[ik] + distances[kj]) { distances[ij] = distances[ik] + distances[kj]; } } } } } int searchMax(int *distances, int size){ int max = 0; for(int i = 0; i<size; i++){ if(distances[i] > max){ max = distances[i]; } } return max; } int main(){ int n; scanf("%d", &n); for(int m = 0; m<n; m++){ int t; scanf("%d", &t); int size = (t*t-t)/2; int* autostrady = new int[size](); int counter = 0; bool zero = false; for (int i = 0; i < t; i++) { for (int j = 0; j < t; j++) { char num; scanf(" %c", &num); if (j < i) { if(num == '0') zero = true; autostrady[counter] = num - '0'; counter++; } } } if(!zero){ printf("1\n"); delete[] autostrady; continue; } int minGlobal = 401; for (int s = 0; s<size; s++){ int* distances = new int[size](); if(autostrady[s] ==0){ autostrady[s] = 1; floydWarshall(autostrady,distances,t); int max = searchMax(distances,size); if(max<minGlobal){ minGlobal = max; } delete[] distances; autostrady[s] = 0; } } printf("%d\n",minGlobal); delete[] autostrady; } 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 | #include <iostream> int index(int i,int j){ return i * (i - 1) / 2 + j; } void floydWarshall(int *autostrady,int *distances, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (autostrady[index(i,j)] == 0 && i != j) { distances[index(i,j)] = 401; } else { distances[index(i,j)] = autostrady[index(i,j)]; } } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { int ij = index(i, j); int ik = (i > k) ? index(i, k) : index(k, i); int kj = (k > j) ? index(k, j) : index(j, k); if (distances[ij] > distances[ik] + distances[kj]) { distances[ij] = distances[ik] + distances[kj]; } } } } } int searchMax(int *distances, int size){ int max = 0; for(int i = 0; i<size; i++){ if(distances[i] > max){ max = distances[i]; } } return max; } int main(){ int n; scanf("%d", &n); for(int m = 0; m<n; m++){ int t; scanf("%d", &t); int size = (t*t-t)/2; int* autostrady = new int[size](); int counter = 0; bool zero = false; for (int i = 0; i < t; i++) { for (int j = 0; j < t; j++) { char num; scanf(" %c", &num); if (j < i) { if(num == '0') zero = true; autostrady[counter] = num - '0'; counter++; } } } if(!zero){ printf("1\n"); delete[] autostrady; continue; } int minGlobal = 401; for (int s = 0; s<size; s++){ int* distances = new int[size](); if(autostrady[s] ==0){ autostrady[s] = 1; floydWarshall(autostrady,distances,t); int max = searchMax(distances,size); if(max<minGlobal){ minGlobal = max; } delete[] distances; autostrady[s] = 0; } } printf("%d\n",minGlobal); delete[] autostrady; } return 0; } |