Closed Manali0806 closed 2 years ago
vector dijkstra(vector<vector> &vec, int vertices, int edges, int source) { vector dist(vertices,INT_MAX); priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; vector<vector<pair<int,int>>> adj(vertices); for(int i=0;i<edges;i++){ adj[vec[i][0]].push_back(make_pair(vec[i][1],vec[i][2])); adj[vec[i][1]].push_back(make_pair(vec[i][0],vec[i][2])); } pq.push(make_pair(0,source)); dist[source]=0; while(pq.size()>0){ int cur = pq.top().second; int distance= pq.top().first; pq.pop(); for(auto node: adj[cur]){ if(dist[cur]!=INT_MAX && dist[cur]+ node.second< dist[node.first]){ dist[node.first]=dist[cur]+node.second; pq.push(make_pair(node.second,node.first)); } } } return dist; }
Please add the solution in a file and link the question, yt and everything in the Reamde
include <bits/stdc++.h>
vector dijkstra(vector<vector> &vec, int vertices, int edges, int source) {
vector dist(vertices,INT_MAX);
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
vector<vector<pair<int,int>>> adj(vertices);
for(int i=0;i<edges;i++){
adj[vec[i][0]].push_back(make_pair(vec[i][1],vec[i][2]));
adj[vec[i][1]].push_back(make_pair(vec[i][0],vec[i][2]));
}
pq.push(make_pair(0,source));
dist[source]=0;
while(pq.size()>0){
int cur = pq.top().second;
int distance= pq.top().first;
pq.pop();
for(auto node: adj[cur]){
if(dist[cur]!=INT_MAX && dist[cur]+ node.second< dist[node.first]){
dist[node.first]=dist[cur]+node.second;
pq.push(make_pair(node.second,node.first));
}
}
}
return dist;
}