You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
/* O(n^3) */
|
|
|
|
|
|
|
|
|
|
void solve(int** adj, int n);
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
int n;
|
|
|
|
|
scanf("%d", &n);
|
|
|
|
|
int **adj = (int**)calloc(n, sizeof(int*));
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
adj[i] = (int*)calloc(n, sizeof(int));
|
|
|
|
|
}
|
|
|
|
|
/* Считываем матрицу смежности. Вместо бесконечности выступает 9999 */
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
|
scanf("%d", &adj[i][j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
solve(adj, n);
|
|
|
|
|
|
|
|
|
|
printf("Решение:\n");
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
|
printf("%d ", adj[i][j]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Освобождаем память */
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
free(adj[i]);
|
|
|
|
|
}
|
|
|
|
|
free(adj);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void solve(int **adj, int n) {
|
|
|
|
|
/* Перебираем все варианты добраться из i в j через k, если выходит короче, то перезаписываем значение i->j */
|
|
|
|
|
for (int k = 0; k < n; k++) {
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
|
if (adj[i][k] + adj[k][j] < adj[i][j]) {
|
|
|
|
|
adj[i][j] = adj[i][k] + adj[k][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|