c_cpp (改进)Dijkstra SSSP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp (改进)Dijkstra SSSP相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>
#define FR(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> ii;

vector< vector<ii> > g;
vi D;
int n;

void dijkstra(int s) {
   D.assign(n, -1);

   priority_queue<ii, vector<ii>, greater<ii> > pq;
   pq.emplace(0, s);

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

      if (D[u] != -1) continue;
      D[u] = d;

      for (auto &i : g[u]) {
	 int v = i.second, w = i.first;
	 pq.emplace(d + w, v);
      }
   }
}

int main() {

   cin >> n;
   g.assign(n, vector<ii>());

   // for each edge
   int m = 5;
   FR(i, m) {
      int u, v, w;
      cin >> u >> v >> w;
      g[u].push_back(ii(w, v));
      g[v].push_back(ii(w, u));
   }

   // run dijkstra from some src
   dijkstra(0);

   // path? for every edge from u -> v, if D[v] == D[u] + weight(u, v),
   // then u -> v is an edge in "some" optimal path.
   
}

以上是关于c_cpp (改进)Dijkstra SSSP的主要内容,如果未能解决你的问题,请参考以下文章

Dijkstra算法

c_cpp Dijkstra算法

c_cpp Dijkstra的邻接矩阵表示算法

c_cpp multiple_source_dijkstra.cpp

Dijkstra 的单源最短路径,带有额外的边,权重为“w”

Dijkstra算法依据项目改进版,输出路径