From 863c15035f796f3268932c12284d1eef6271e206 Mon Sep 17 00:00:00 2001 From: anqude Date: Sat, 31 May 2025 06:38:47 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20main.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..1e5c370 --- /dev/null +++ b/main.cpp @@ -0,0 +1,76 @@ +#include +#include +using namespace std; + +void printMatrix(const vector>& matrix, int k) +{ + cout << "W" << k << ":\n"; + int n = matrix.size(); + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + cout << matrix[i][j] << " "; + } + cout << "\n"; + } + cout << "\n"; +} + +void Warshall(vector>& graph, int n) +{ + vector> W = graph; // Копия исходной матрицы + + printMatrix(W, 0); // W0 или M + + for (int k = 0; k < n; ++k) + { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) + { + W[i][j] = W[i][j] or (W[i][k] and W[k][j]); + } + } + printMatrix(W, k + 1); // W1 ... Wn + } +} + +int main() { + int n; + cout << "Введите размер матрицы смежности: "; + cin >> n; + while (n <= 0) + { + cout << "Введите корректный размер: "; + cin >> n; + } + vector> graph(n, vector(n)); + + cout << "Введите матрицу смежности (" << n << " строк по " << n << " чисел):\n"; + + for (int i = 0; i < n; ++i) + { + cout<<"Строка " << i+1<<": "; + for (int j = 0; j < n; ++j) + { + int value; + while (true) + { + cin >> value; + if (value != 0 && value != 1) + { + cout << "Некорректное значение. Введите 0 или 1: "; + } + else + { + break; + } + } + graph[i][j] = value; + } + } + cout << "\n"; + Warshall(graph, n); + + return 0; +} \ No newline at end of file