带邻接列表映射的 Dijkstra 算法 c++
Dijkstra's Algorithm w/ Adjacency List Map c++
目前正在尝试通过在读入地图对象的文本文件中使用邻接表,在 C++ 中实现 dijkstra 算法。地图初始化为:
map<int, vector<pair<int, int>>> = adjList;
示例文本文件输入:
1 2,1 8,2
2 1,1 3,1
3 2,1 4,1
4 3,1 5,1
5 4,1 6,1
6 5,1 7,1
7 6,1 8,1
8 7,1 1,2
其中键是一个顶点,向量中对的x值连接到键顶点。 y 值是路径距离。我将该映射传递到我的 dijkstra 函数中,在该函数中我初始化了一个用于最短距离的向量和一个用于存储已访问顶点的向量。我的循环是事情开始出错的地方,因为我得到零和非常大的数字的输出。这是我的代码:
//checks if vertex has been visited or not
bool booler(int vertex, vector<int> visited){
bool booly;
if(find(visited.begin(), visited.end(), vertex) != visited.end()){
booly = true;
}
else{
booly = false;
}
return booly;
}
//checks vector for the shortest distance vertex
int minDist(vector<int> distances, vector<int> visited){
int minDist = 1000000;
int index;
for(int v = 0; v < distances.size(); v++){
if(booler(v, visited) == false && distances[v] < minDist){
minDist = distances[v];
index = v;
}
}
return index;
}
void dijkstra(int source, map<int, vector<pair<int, int>>> adjList, int vSize){
vector<int> distances(vSize, 1000000);
vector<int> visited = {};
distances[source] = 0;
for(int c = 0; c < distances.size(); c++){
int u = minDist(distances, visited);
visited.push_back(u);
for(int v = 1; v < distances.size(); v++){
for(int s = 0; s < adjList[u].size(); s++){
//updates distances based on v connection to u
if(booler(v, visited) == false && distances[u] < 1000000 && adjList[u][s].second + distances[u] < distances[v]){
distances[v] = distances[u] + adjList[u][v].second;
}
}
}
}
//prints out shortest path
for(int x = 0; x < distances.size(); x++){
cout << distances[x] << " " << endl;
}
}
我无法修复此错误,如有任何帮助,我们将不胜感激!
这里是一个如何使用 dijkstra 的工具。
这是我解决你问题的方法:
#include "bits/stdc++.h"
using namespace std;
map<int, vector<pair<int, int> > > mp;
void addEdge(int u, int v, int dist) {
mp[u].push_back(make_pair(v, dist));
}
void startDijkstra(int u) {
vector<int> dist(1e2 + 1, 1e9);
set<pair<int, int> > st;
st.insert(make_pair(0, u));
dist[u] = 0;
while (!st.empty()) {
pair<int, int> now = *st.begin();
st.erase(st.begin());
int v = now.second;
int w = now.first;
const vector<pair<int, int> > &edges = mp[v];
for (const pair<int, int> &to : edges) {
if (w + to.second < dist[to.first]) {
st.erase(make_pair(dist[to.first], to.first));
dist[to.first] = w + to.second;
st.insert(make_pair(dist[to.first], to.first));
}
}
}
for (int i = 1; i <= 8; i++) {
cout << i << ' ' << dist[i] << endl;
}
}
int main() {
addEdge(1, 2, 1);
addEdge(1, 8, 2);
addEdge(2, 1, 1);
addEdge(2, 3, 1);
addEdge(3, 2, 1);
addEdge(3, 4, 1);
addEdge(4, 3, 1);
addEdge(4, 5, 1);
addEdge(5, 4, 1);
addEdge(5, 6, 1);
addEdge(6, 5, 1);
addEdge(6, 7, 1);
addEdge(7, 6, 1);
addEdge(7, 8, 1);
addEdge(8, 9, 1);
addEdge(8, 1, 2);
startDijkstra(1);
return 0;
}
目前正在尝试通过在读入地图对象的文本文件中使用邻接表,在 C++ 中实现 dijkstra 算法。地图初始化为:
map<int, vector<pair<int, int>>> = adjList;
示例文本文件输入:
1 2,1 8,2
2 1,1 3,1
3 2,1 4,1
4 3,1 5,1
5 4,1 6,1
6 5,1 7,1
7 6,1 8,1
8 7,1 1,2
其中键是一个顶点,向量中对的x值连接到键顶点。 y 值是路径距离。我将该映射传递到我的 dijkstra 函数中,在该函数中我初始化了一个用于最短距离的向量和一个用于存储已访问顶点的向量。我的循环是事情开始出错的地方,因为我得到零和非常大的数字的输出。这是我的代码:
//checks if vertex has been visited or not
bool booler(int vertex, vector<int> visited){
bool booly;
if(find(visited.begin(), visited.end(), vertex) != visited.end()){
booly = true;
}
else{
booly = false;
}
return booly;
}
//checks vector for the shortest distance vertex
int minDist(vector<int> distances, vector<int> visited){
int minDist = 1000000;
int index;
for(int v = 0; v < distances.size(); v++){
if(booler(v, visited) == false && distances[v] < minDist){
minDist = distances[v];
index = v;
}
}
return index;
}
void dijkstra(int source, map<int, vector<pair<int, int>>> adjList, int vSize){
vector<int> distances(vSize, 1000000);
vector<int> visited = {};
distances[source] = 0;
for(int c = 0; c < distances.size(); c++){
int u = minDist(distances, visited);
visited.push_back(u);
for(int v = 1; v < distances.size(); v++){
for(int s = 0; s < adjList[u].size(); s++){
//updates distances based on v connection to u
if(booler(v, visited) == false && distances[u] < 1000000 && adjList[u][s].second + distances[u] < distances[v]){
distances[v] = distances[u] + adjList[u][v].second;
}
}
}
}
//prints out shortest path
for(int x = 0; x < distances.size(); x++){
cout << distances[x] << " " << endl;
}
}
我无法修复此错误,如有任何帮助,我们将不胜感激!
这里是一个如何使用 dijkstra 的工具。
这是我解决你问题的方法:
#include "bits/stdc++.h"
using namespace std;
map<int, vector<pair<int, int> > > mp;
void addEdge(int u, int v, int dist) {
mp[u].push_back(make_pair(v, dist));
}
void startDijkstra(int u) {
vector<int> dist(1e2 + 1, 1e9);
set<pair<int, int> > st;
st.insert(make_pair(0, u));
dist[u] = 0;
while (!st.empty()) {
pair<int, int> now = *st.begin();
st.erase(st.begin());
int v = now.second;
int w = now.first;
const vector<pair<int, int> > &edges = mp[v];
for (const pair<int, int> &to : edges) {
if (w + to.second < dist[to.first]) {
st.erase(make_pair(dist[to.first], to.first));
dist[to.first] = w + to.second;
st.insert(make_pair(dist[to.first], to.first));
}
}
}
for (int i = 1; i <= 8; i++) {
cout << i << ' ' << dist[i] << endl;
}
}
int main() {
addEdge(1, 2, 1);
addEdge(1, 8, 2);
addEdge(2, 1, 1);
addEdge(2, 3, 1);
addEdge(3, 2, 1);
addEdge(3, 4, 1);
addEdge(4, 3, 1);
addEdge(4, 5, 1);
addEdge(5, 4, 1);
addEdge(5, 6, 1);
addEdge(6, 5, 1);
addEdge(6, 7, 1);
addEdge(7, 6, 1);
addEdge(7, 8, 1);
addEdge(8, 9, 1);
addEdge(8, 1, 2);
startDijkstra(1);
return 0;
}