Обновить main.cpp
This commit is contained in:
parent
165ae55a3c
commit
0e050969c8
101
main.cpp
101
main.cpp
@ -1,91 +1,86 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <iomanip>
|
#include <queue>
|
||||||
|
#include <algorithm>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const int INF = numeric_limits<int>::max(); // Представляет "бесконечность"
|
const int INF = numeric_limits<int>::max();
|
||||||
|
|
||||||
// Функция для поиска вершины с минимальным расстоянием
|
// Функция алгоритма Дейкстры
|
||||||
int minDistance(const vector<int>& dist, const vector<bool>& visited) {
|
void dijkstra(const vector<vector<int>>& graph, int start, int end, int n) {
|
||||||
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> dist(n, INF); // Расстояния до вершин
|
||||||
vector<int> parent(n, -1); // Для восстановления пути
|
vector<int> prev(n, -1); // Предыдущие вершины для восстановления пути
|
||||||
vector<bool> visited(n, false); // Посещённые вершины
|
vector<bool> visited(n, false); // Посещённые вершины
|
||||||
|
|
||||||
dist[start] = 0;
|
dist[start] = 0;
|
||||||
|
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
|
||||||
|
pq.push({0, start});
|
||||||
|
|
||||||
for (int count = 0; count < n - 1; ++count) {
|
while (!pq.empty()) {
|
||||||
int u = minDistance(dist, visited);
|
int u = pq.top().second;
|
||||||
if (u == -1) break; // Недостижимые узлы
|
pq.pop();
|
||||||
|
|
||||||
|
if (visited[u]) continue;
|
||||||
visited[u] = true;
|
visited[u] = true;
|
||||||
|
|
||||||
for (int v = 0; v < n; ++v)
|
for (int v = 0; v < n; ++v) {
|
||||||
if (!visited[v] && graph[u][v] && dist[u] != INF && dist[u] + graph[u][v] < dist[v]) {
|
if (graph[u][v] != -1 && !visited[v]) {
|
||||||
dist[v] = dist[u] + graph[u][v];
|
int newDist = dist[u] + graph[u][v];
|
||||||
parent[v] = u;
|
if (newDist < dist[v]) {
|
||||||
|
dist[v] = newDist;
|
||||||
|
prev[v] = u;
|
||||||
|
pq.push({dist[v], v});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Восстанавливаем путь
|
|
||||||
if (dist[end] == INF) {
|
if (dist[end] == INF) {
|
||||||
cout << "Путь из вершины " << start << " в вершину " << end << " не существует." << endl;
|
cout << "Путь не существует." << endl;
|
||||||
return;
|
} else {
|
||||||
}
|
cout << "Кратчайший путь: ";
|
||||||
|
|
||||||
vector<int> path;
|
vector<int> path;
|
||||||
int current = end;
|
for (int v = end; v != -1; v = prev[v])
|
||||||
while (current != -1) {
|
path.push_back(v);
|
||||||
path.push_back(current);
|
|
||||||
current = parent[current];
|
reverse(path.begin(), path.end());
|
||||||
|
for (size_t i = 0; i < path.size(); ++i) {
|
||||||
|
cout << path[i]+1;
|
||||||
|
if (i != path.size() - 1)
|
||||||
|
cout << " -> ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
cout << "Суммарная стоимость: " << dist[end] << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
int main() {
|
||||||
setlocale(LC_ALL, "Russian");
|
setlocale(LC_ALL, "ru");
|
||||||
|
|
||||||
int n;
|
int n;
|
||||||
cout << "Введите количество вершин: ";
|
cout << "Введите количество вершин графа: ";
|
||||||
cin >> n;
|
cin >> n;
|
||||||
|
|
||||||
vector<vector<int>> graph(n, vector<int>(n));
|
vector<vector<int>> graph(n, vector<int>(n));
|
||||||
|
|
||||||
cout << "Введите матрицу смежности (веса, 0 - отсутствие ребра):" << endl;
|
cout << "Введите матрицу смежности (" << -1 << " — нет ребра, 0 — петля, >0 — вес):" << endl;
|
||||||
for (int i = 0; i < n; ++i)
|
for (int i = 0; i < n; ++i) {
|
||||||
for (int j = 0; j < n; ++j)
|
for (int j = 0; j < n; ++j) {
|
||||||
cin >> graph[i][j];
|
cin >> graph[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int start, end;
|
int start, end;
|
||||||
cout << "Введите начальную вершину: ";
|
cout << "Начальная и конечная вершины (1-" << n << "): ";
|
||||||
cin >> start;
|
cin >> start >> end;
|
||||||
cout << "Введите конечную вершину: ";
|
|
||||||
cin >> end;
|
|
||||||
|
|
||||||
if (start < 0 || start >= n || end < 0 || end >= n) {
|
if (start < 1 || start >= n+1 || end < 1 || end >= n+1) {
|
||||||
cout << "Ошибка: номер вершины вне диапазона!" << endl;
|
cout << "Некорректный ввод вершин." << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
dijkstra(graph, start, end);
|
dijkstra(graph, start-1, end-1, n);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user