C++ 具有节点访问代价的Dijkstra算法?

C++ 具有节点访问代价的Dijkstra算法?,c++,dijkstra,C++,Dijkstra,所以我一直在关注这个 所以我想补充一点,每个节点都有某种数量或价格,这是我的代码: #include <iostream> #include <bits/stdc++.h> #include <vector> #define INF 0x3f3f3f3f typedef std::pair<int, int> iPair; std::unordered_map<int, int> hashTable;

所以我一直在关注这个

所以我想补充一点,每个节点都有某种数量或价格,这是我的代码:

  #include <iostream>
  #include <bits/stdc++.h>
  #include <vector>

  #define INF 0x3f3f3f3f

  typedef std::pair<int, int> iPair;

  std::unordered_map<int, int> hashTable;

  class Graph
  {
    int V;
    std::list<std::pair<int, int>> *adj;

  public:
    Graph(int V);
    void addEdge(int u, int v, int w);
    void shortestPath(int s, int end);
  };

  Graph::Graph(int V)
  {
    this->V = V;
    adj = new std::list<iPair>[V];
  }

  void Graph::addEdge(int u, int v, int w)
  {
    adj[u].push_back(std::make_pair(v, w));
    adj[v].push_back(std::make_pair(u, w));
  }

  void Graph::shortestPath(int src, int end)
  {
    std::priority_queue<iPair, std::vector<iPair>, std::greater<iPair>> pq;

    std::vector<int> dist(V, INF);

    pq.push(std::make_pair(0, src));
    dist[src] = 0;

    while (!pq.empty())
    {
      int u = pq.top().second;
      pq.pop();

      std::list<std::pair<int, int>>::iterator i;
      for (i = adj[u].begin(); i != adj[u].end(); ++i)
      {
        int v = (*i).first;
        int weight = (*i).second;

        if (dist[v] > dist[u] + weight + hashTable[v])
        {
          dist[v] = dist[u] + weight + hashTable[v];
          pq.push(std::make_pair(dist[v], v));
        }
      }
    }
    printf("Vertex   Distance from Source\n");
    for (int i = 0; i < V; ++i)
      printf("%d \t\t %d\n", i, dist[i]);
  }
  int main()
  {
    int V = 9;
    Graph g(V);

    g.addEdge(0, 1, 4);
    g.addEdge(0, 7, 8);
    g.addEdge(1, 2, 8);
    g.addEdge(1, 7, 11);
    g.addEdge(2, 3, 7);
    g.addEdge(2, 8, 2);
    g.addEdge(2, 5, 4);
    g.addEdge(3, 4, 9);
    g.addEdge(3, 5, 14);
    g.addEdge(4, 5, 10);
    g.addEdge(5, 6, 2);
    g.addEdge(6, 7, 1);
    g.addEdge(6, 8, 6);
    g.addEdge(7, 8, 7);

    hashTable[2] = 30;

    g.shortestPath(0, 2);

    return 0;
  }
这是通过哈希表输出的:

0          0
1          4
2          12
3          19
4          21
5          11
6          9
7          8
8          14
0                0
1                4
2                42
3                25
4                21
5                11
6                9
7                8
8                15

有人能解释一下为什么这不起作用吗?我对算法这个话题还很陌生,所以我试图理解它。

为什么
hashTable[2]=30这30是12的额外值,它为您提供了42个,但为什么id 8上的节点在哈希表之前有14个,而它却有15个成本?