-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastDijkstra.cpp
54 lines (46 loc) · 1.34 KB
/
FastDijkstra.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Implementation of Dijkstra's algorithm using adjacency lists
// and priority queue for efficiency.
//
// Running time: O(|E| log |V|)
#include <queue>
#include <stdio.h>
using namespace std;
const int INF = 2000000000;
typedef pair<int,int> PII;
int main(){
int N, s, t;
scanf ("%d%d%d", &N, &s, &t);
vector<vector<PII> > edges(N);
for (int i = 0; i < N; i++){
int M;
scanf ("%d", &M);
for (int j = 0; j < M; j++){
int vertex, dist;
scanf ("%d%d", &vertex, &dist);
edges[i].push_back (make_pair (dist, vertex)); // note order of arguments here
}
}
// use priority queue in which top element has the "smallest" priority
priority_queue<PII, vector<PII>, greater<PII> > Q;
vector<int> dist(N, INF), dad(N, -1);
Q.push (make_pair (0, s));
dist[s] = 0;
while (!Q.empty()){
PII p = Q.top();
if (p.second == t) break;
Q.pop();
int here = p.second;
for (vector<PII>::iterator it=edges[here].begin(); it!=edges[here].end(); it++){
if (dist[here] + it->first < dist[it->second]){
dist[it->second] = dist[here] + it->first;
dad[it->second] = here;
Q.push (make_pair (dist[it->second], it->second));
}
}
}
printf ("%d\n", dist[t]);
if (dist[t] < INF)
for(int i=t;i!=-1;i=dad[i])
printf ("%d%c", i, (i==s?'\n':' '));
return 0;
}