【问题标题】:Dijkstra algorithm with Adjacency lists具有邻接表的 Dijkstra 算法
【发布时间】:2014-04-01 16:15:56
【问题描述】:

所以我一直在尝试使用邻接列表在有向图中实现最短路径的 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 算法的想法)。

提前致谢。

劳尔

【问题讨论】:

  • 我见过的更好的问题/答案之一,涵盖了用 C++ 实现的算法can be found here。您可能会发现一些有用的答案。
  • 我之所以选择发布这个问题是因为我确实实现了正确的算法。它取自 TopCoder。所以这不是问题。问题是在我的代码中的某个地方发生了一些事情,导致它打印错误的结果。我认为这是因为使用了动态类型 list,更具体地说是 vector >,以及 priority_queue >。我的意思是,很可能我在某处有一个逻辑错误,与这些“复杂”数据结构的使用有关(并不复杂,但比 O(N^2) 复杂度的简单 Dijkstra 方法复杂得多)
  • 查看了您附加的链接。这不是一个很好的解释,至少不如 TopCoder 上的解释好(其中解释了整个想法,并以 3 种不同的方式实现 - 标准、priority_queue >、set -但我仍然无法弄清楚为什么我的程序无法正常运行(即:打印正确的结果)。如果有人可以帮助我,我将非常感激。提前致谢。
  • 有趣,因为其中一个答案来自 TopCoder。
  • 嗯,嗯,我看了一下代码,好像不是这样的:vi D(N, 987654321); priority_queue,greater > Q; D[0] = 0; Q.push(ii(0,0)); while(!Q.empty()) { ii top = Q.top(); Q.pop(); int v = top.second,d = top.first; if(d first, cost = it->second;如果(D[v2] > D[v] + 成本){ D[v2] = D[v] + 成本; //等等。

标签: c++ path structure dijkstra shortest


【解决方案1】:

问题出在一行

weights.resize(nrVertices+1);

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

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

将行替换为

weights.resize(1);

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

【讨论】:

  • 是的,非常感谢先生。我没有对代码的那部分给予足够的重视。我认为问题来自于使用 priority_queue 或动态列表。再次感谢。我真的很感激:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-12
相关资源
最近更新 更多