Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int a[100][100]; // Матрица смежности
- int c[100][100]; // Матрица стоимостей
- void main() {
- setlocale(LC_ALL, "Russian");
- int n;
- cout << "Введите колво вершин" << endl;
- cin >> n;
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++)
- c[i][j] = 10000;
- cout << "Вводите смежные вершины (формат: <v1> <v2> <стоимость>, окончание ввода - 0 0 0)" << endl;
- int v1, v2, cost;
- cin >> v1 >> v2 >> cost;
- while (v1 != 0 && v2 != 0) {
- c[v1][v2] = cost;
- c[v2][v1] = cost; // Убрать, если граф ориентированый.
- cin >> v1 >> v2 >> cost;
- }
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++)
- if (i == j)
- a[i][j] = 0;
- else
- a[i][j] = c[i][j];
- //Конец инициализации, начало самого алгоритма
- for (int k = 1; k <= n; k++)
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++)
- if (a[i][k] + a[k][j] < a[i][j])
- a[i][j] = a[i][k] + a[k][j];
- // Определение центра графа:
- int d[100] = { 0 };
- for (int j = 1; j <= n; j++) {
- int max = 0;
- for (int i = 1; i <= n; i++) {
- if (a[i][j] > max)
- max = a[i][j];
- }
- d[j] = max;
- }
- int min = 1000;
- for (int i = 1; i <= n; i++)
- if (d[i] < min)
- min = d[i];
- cout << "Центр: ";
- for (int i = 1; i <= n; i++)
- if (d[i] == min)
- cout << i << " ";
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement