具有邻接列表的Dijkstra算法

时间:2014-02-28 15:16:22

标签: c++ path structure dijkstra shortest

所以我一直在尝试使用邻接列表在有向图中实现最短路径的Dijkstra算法,但是我不知道是什么原因,它不会打印出结果(打印最小距离为0到所有节点)。

我写的代码是:

#include <fstream>
#include <functional>
#include <climits>
#include <vector>
#include <queue>
#include <list>

using namespace std;

struct node {
    int vertex;
    int weight;
    node(int v, int w) : vertex(v), weight(w) { };
    node() { }
};

class CompareGreater {
    public:
        bool const operator()(node &nodeX, node &nodeY) {
            return (nodeX.weight > nodeY.weight) ;
        }
};

vector< list<node> > adj;
vector<int> weights;
priority_queue<node, vector<node>, CompareGreater> Q;

int nrVertices, nrEdges;

void readData();
void Dijkstra(node);
void writeData();

int main(int argc, char *argv[]) {

    readData();
    Dijkstra(node(1, 0));
    writeData();

    return 0;
}

void readData() {
    fstream in("dijkstra.in", ios::in);

    int nodeX, nodeY, weight;

    in >> nrVertices >> nrEdges;

    adj.resize(nrVertices+1);
    weights.resize(nrVertices+1);

    for (int i = 1; i <= nrVertices; ++i) {
        weights.push_back(INT_MAX);
    }

    for (int i = 1; i <= nrEdges; ++i) {
        in >> nodeX >> nodeY >> weight;
        adj[nodeX].push_back(node(nodeY, weight));
    }

    in.close();
}

void Dijkstra(node startNode) {
    node currentNode;

    weights[startNode.vertex] = 0;
    Q.push(startNode);

    while (!Q.empty()) {
        currentNode = Q.top();
        Q.pop();

        if (currentNode.weight <= weights[currentNode.vertex]) {
            for (list<node>::iterator it = adj[currentNode.vertex].begin(); it != adj[currentNode.vertex].end(); ++it) {
                if (weights[it->vertex] > weights[currentNode.vertex] + it->weight) {
                    weights[it->vertex] = weights[currentNode.vertex] + it->weight;
                    Q.push(node((it->vertex), weights[it->vertex]));
                }
            }
        }
    }
}

void writeData() {
    fstream out("dijkstra.out", ios::out);

    weights.resize(nrVertices+1);
    for (vector<int>::iterator it = weights.begin()+1; it != weights.end(); ++it) {
        out << (*it) << " ";
    }

    out.close();
}

输入数据是:

5 7
1 2 10
1 3 2
1 5 100
2 4 3
3 2 5
4 3 15
4 5 5

这意味着有5个节点,7个弧(有向边),并且弧从节点1到2存在,成本为10,从1到3,成本为2,依此类推。

但输出错误。我不知道程序可能会失败的地方。我从这里接受了主要想法:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=standardTemplateLibrary2#dijkstra1 (最后,它使用priority_queue给出了Dijkstra算法的想法)。

提前致谢。

劳尔

1 个答案:

答案 0 :(得分:2)

问题出在

weights.resize(nrVertices+1);
readData()中的

。这将设置一个值为nrVertices+1元素的向量。稍后,使用weights.push_back(INT_MAX);将所需的实际值附加到此向量。

在实际的Dijkstra算法中,所有有趣的weights都是0,而不是你想要的INT_MAX

替换该行
weights.resize(1);

(只是为了确保索引1真正引用第一个元素 - 你似乎使用1作为第一个索引而不是0),它可能会起作用。