Question: Task 0 : #include #include #include #include using namespace std; typedef pair iPair; void addEdge ( vector > adj [ ] , int u ,
Task : #include
#include
#include
#include
using namespace std;
typedef pair iPair;
void addEdgevector adj int u int v int wt
adjupushbackv wt;
adjvpushbacku wt; for undirected graph
void shortestPathvector adj int V int src
Initialize priority queue to store vertices with their distances
priorityqueue, greater pq;
Initialize distances array to store shortest distances from source
vector distV numericlimits::max;
Insert source vertex into priority queue with distance
pqpush src;
distsrc;
Dijkstra's algorithm
while pqempty
Extract the vertex with the minimum distance from the priority queue
int u pqtopsecond;
pqpop;
Update distances of adjacent vertices
for auto& edge : adju
int v edge.first;
int weight edge.second;
If a shorter path is found, update the distance and insert vertex into priority queue
if distu weight distv
distv distu weight;
pqpushdistv v;
Output shortest paths from source vertex to all other vertices
cout "Shortest paths from source vertex src to all other vertices:
;
for int i ; i V; i
cout "Vertex i : Distance disti
;
int main
Graph representation
vector adj;
addEdgeadj;
addEdgeadj;
addEdgeadj;
addEdgeadj;
addEdgeadj;
addEdgeadj;
addEdgeadj;
addEdgeadj;
Source vertex
int src ;
Number of vertices
int V ;
Find shortest paths from source vertex
shortestPathadj V src;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
