Question: ( I am getting Vertex 0 and 1 wrong for Vertex 0 i need to ger 4 and Vertex 1 i need to get 3
I am getting Vertex and wrong for Vertex i need to ger and Vertex i need to get but for both of thoses vertexes i keep getting no path This is my code below: #include
#include
#include
#include
using namespace std;
vector dijkstraint V vector adj, int S vector& dist
dist.resizeV INTMAX; disti will hold the shortest distance from S to i
vector prevV; previ will hold the penultimate vertex in the shortest path from S to i
Priority queue to store vertices that are being processed
priorityqueue, vector greater pq;
Initialize source vertex distance as and push it to the priority queue
distS; Adjusted: Set distance from source to itself as
pqpush S;
Dijkstra's algorithm
while pqempty
int u pqtopsecond;
pqpop;
Visit each neighbor of u
for auto& edge : adju
int v edge.first; neighbor vertex
int weight edge.second; weight of the edge from u to v
If a shorter path is found, update distance and penultimate vertex
if distu weight distv
distv distu weight;
prevv u;
pqpushdistv v;
return prev; Return the penultimate vertex array
int main
int v e source;
cout "Enter the number of vertices and edges: ;
cin v e;
Initialize the graph with n vertices
vector adjv;
Read edges and weights
cout "Enter edges in the format from to weight: endl;
for int i ; i e; i
int from, to weight;
cin from to weight;
adjfrompushbackto weight;
cout "Enter the source vertex: ;
cin source;
Find shortest paths from source to all vertices
vector dist;
vector prevvertices dijkstrav adj, source, dist;
Print shortest paths
cout "Shortest paths from source vertex source :
;
for int i ; i v; i
cout "Vertex i : ;
if disti INTMAX
cout No path
;
else
cout disti endl;
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
