Добавить main.cpp
This commit is contained in:
parent
b6525ee6db
commit
165ae55a3c
91
main.cpp
Normal file
91
main.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int INF = numeric_limits<int>::max(); // Представляет "бесконечность"
|
||||
|
||||
// Функция для поиска вершины с минимальным расстоянием
|
||||
int minDistance(const vector<int>& dist, const vector<bool>& visited) {
|
||||
int minDist = INF;
|
||||
int minIndex = -1;
|
||||
|
||||
for (size_t i = 0; i < dist.size(); ++i)
|
||||
if (!visited[i] && dist[i] < minDist)
|
||||
minDist = dist[i], minIndex = i;
|
||||
|
||||
return minIndex;
|
||||
}
|
||||
|
||||
// Алгоритм Дейкстры
|
||||
void dijkstra(const vector<vector<int>>& graph, int start, int end) {
|
||||
int n = graph.size();
|
||||
vector<int> dist(n, INF); // Расстояния до вершин
|
||||
vector<int> parent(n, -1); // Для восстановления пути
|
||||
vector<bool> visited(n, false); // Посещённые вершины
|
||||
|
||||
dist[start] = 0;
|
||||
|
||||
for (int count = 0; count < n - 1; ++count) {
|
||||
int u = minDistance(dist, visited);
|
||||
if (u == -1) break; // Недостижимые узлы
|
||||
visited[u] = true;
|
||||
|
||||
for (int v = 0; v < n; ++v)
|
||||
if (!visited[v] && graph[u][v] && dist[u] != INF && dist[u] + graph[u][v] < dist[v]) {
|
||||
dist[v] = dist[u] + graph[u][v];
|
||||
parent[v] = u;
|
||||
}
|
||||
}
|
||||
|
||||
// Восстанавливаем путь
|
||||
if (dist[end] == INF) {
|
||||
cout << "Путь из вершины " << start << " в вершину " << end << " не существует." << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
vector<int> path;
|
||||
int current = end;
|
||||
while (current != -1) {
|
||||
path.push_back(current);
|
||||
current = parent[current];
|
||||
}
|
||||
|
||||
cout << "Кратчайший путь из вершины " << start << " в вершину " << end << ":" << endl;
|
||||
for (auto it = path.rbegin(); it != path.rend(); ++it)
|
||||
cout << *it << (next(it) != path.rend() ? " -> " : "");
|
||||
|
||||
cout << "\nСуммарная стоимость пути: " << dist[end] << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
setlocale(LC_ALL, "Russian");
|
||||
|
||||
int n;
|
||||
cout << "Введите количество вершин: ";
|
||||
cin >> n;
|
||||
|
||||
vector<vector<int>> graph(n, vector<int>(n));
|
||||
|
||||
cout << "Введите матрицу смежности (веса, 0 - отсутствие ребра):" << endl;
|
||||
for (int i = 0; i < n; ++i)
|
||||
for (int j = 0; j < n; ++j)
|
||||
cin >> graph[i][j];
|
||||
|
||||
int start, end;
|
||||
cout << "Введите начальную вершину: ";
|
||||
cin >> start;
|
||||
cout << "Введите конечную вершину: ";
|
||||
cin >> end;
|
||||
|
||||
if (start < 0 || start >= n || end < 0 || end >= n) {
|
||||
cout << "Ошибка: номер вершины вне диапазона!" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
dijkstra(graph, start, end);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user