带邻接表映射的Dijkstra算法c ++

时间:2018-11-07 06:30:12

标签: c++ graph-theory dijkstra stdmap adjacency-list

当前,尝试通过使用读入地图对象的文本文件中的邻接表在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;
    }

}

我无法修复此错误,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

这是一个如何使用dijkstra的工具。

https://www.quora.com/What-is-the-most-simple-efficient-C++-code-for-Dijkstras-shortest-path-algorithm

这是我为您解决的问题的解决方案

#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;
}