|
| 1 | +// Program to find Dijkstra's shortest path using |
| 2 | +// priority_queue in STL |
| 3 | +#include<bits/stdc++.h> |
| 4 | +using namespace std; |
| 5 | +# define INF 0x3f3f3f3f |
| 6 | + |
| 7 | +// iPair ==> Integer Pair |
| 8 | +typedef pair<int, int> iPair; |
| 9 | + |
| 10 | +// To add an edge |
| 11 | +void addEdge(vector <pair<int, int> > adj[], int u, |
| 12 | + int v, int wt) |
| 13 | +{ |
| 14 | + adj[u].push_back(make_pair(v, wt)); |
| 15 | + adj[v].push_back(make_pair(u, wt)); |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +// Prints shortest paths from src to all other vertices |
| 20 | +void shortestPath(vector<pair<int,int> > adj[], int V, int src) |
| 21 | +{ |
| 22 | + // Create a priority queue to store vertices that |
| 23 | + // are being preprocessed. This is weird syntax in C++. |
| 24 | + priority_queue< iPair, vector <iPair> , greater<iPair> > pq; |
| 25 | + |
| 26 | + // Create a vector for distances and initialize all |
| 27 | + // distances as infinite (INF) |
| 28 | + vector<int> dist(V, INF); |
| 29 | + |
| 30 | + // Insert source itself in priority queue and initialize |
| 31 | + // its distance as 0. |
| 32 | + pq.push(make_pair(0, src)); |
| 33 | + dist[src] = 0; |
| 34 | + |
| 35 | + /* Looping till priority queue becomes empty (or all |
| 36 | + distances are not finalized) */ |
| 37 | + while (!pq.empty()) |
| 38 | + { |
| 39 | + // The first vertex in pair is the minimum distance |
| 40 | + // vertex, extract it from priority queue. |
| 41 | + // vertex label is stored in second of pair (it |
| 42 | + // has to be done this way to keep the vertices |
| 43 | + // sorted distance (distance must be first item |
| 44 | + // in pair) |
| 45 | + int u = pq.top().second; |
| 46 | + pq.pop(); |
| 47 | + |
| 48 | + // Get all adjacent of u. |
| 49 | + for (auto x : adj[u]) |
| 50 | + { |
| 51 | + // Get vertex label and weight of current adjacent |
| 52 | + // of u. |
| 53 | + int v = x.first; |
| 54 | + int weight = x.second; |
| 55 | + |
| 56 | + // If there is shorted path to v through u. |
| 57 | + if (dist[v] > dist[u] + weight) |
| 58 | + { |
| 59 | + // Updating distance of v |
| 60 | + dist[v] = dist[u] + weight; |
| 61 | + pq.push(make_pair(dist[v], v)); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Print shortest distances stored in dist[] |
| 67 | + printf("Vertex Distance from Source\n"); |
| 68 | + for (int i = 0; i < V; ++i) |
| 69 | + printf("%d \t\t %d\n", i, dist[i]); |
| 70 | +} |
| 71 | + |
| 72 | +// Driver program to test methods of graph class |
| 73 | +int main() |
| 74 | +{ |
| 75 | + int V = 9; |
| 76 | + vector<iPair > adj[V]; |
| 77 | + |
| 78 | + // making above shown graph |
| 79 | + addEdge(adj, 0, 1, 4); |
| 80 | + addEdge(adj, 0, 7, 8); |
| 81 | + addEdge(adj, 1, 2, 8); |
| 82 | + addEdge(adj, 1, 7, 11); |
| 83 | + addEdge(adj, 2, 3, 7); |
| 84 | + addEdge(adj, 2, 8, 2); |
| 85 | + addEdge(adj, 2, 5, 4); |
| 86 | + addEdge(adj, 3, 4, 9); |
| 87 | + addEdge(adj, 3, 5, 14); |
| 88 | + addEdge(adj, 4, 5, 10); |
| 89 | + addEdge(adj, 5, 6, 2); |
| 90 | + addEdge(adj, 6, 7, 1); |
| 91 | + addEdge(adj, 6, 8, 6); |
| 92 | + addEdge(adj, 7, 8, 7); |
| 93 | + shortestPath(adj, V, 0); |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
0 commit comments