Блог пользователя fluffy_x

Автор fluffy_x, история, 8 лет назад, По-английски

How to find the shortest path in a weighted graph(**if many then lexicographically smallest**) using Dijkstra? — I am getting WA on 2 test cases - Problem link :Your text to link here... - Although this can be solved using BFS, but I want to know do when the graph is weighted?

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Let's assume X = 1. Using Dijkstra, calculate the distance Dv from node 1 to node v. For every node u, store Pu, the minimum v over all edges (v, u) such that Dv + w(v, u) = Du. It is easy to find this path now:

std::vector<int> find_path(int v) {
	std::vector<int> ans;
	for(ans.push_back(v); v != 1; ans.push_back(v = p[v]));
	std::reverse(ans.begin(), ans.end());
        return ans;
}