-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
44 lines (44 loc) · 1.13 KB
/
dijkstra.cpp
File metadata and controls
44 lines (44 loc) · 1.13 KB
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
#include<bits/stdc++.h>
using namespace std;
class Edge{
int v;
int wt;
public:
Edge(int v, int wt){
this->v=v;
this->wt=wt;
}
void dijkstras(vector<vector<Edge>> &graph, int src) {
vector<int> dist(graph.size(), INT_MAX);
dist[src] = 0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,src});
while(!pq.empty()){
int u=pq.top().second;
pq.pop();
vector<Edge> edges=graph[u];
for(auto edge: edges){
if(dist[edge.v]>dist[u]+edge.wt)
{
dist[edge.v]=dist[u]+edge.wt;
pq.push({dist[edge.wt], edge.v});
}
}
}
for(auto d: dist)
cout<<d<<" ";
}
};
int main(){
vector<vector<Edge>> graph(6);
graph[0].push_back(Edge(1,2));
graph[0].push_back(Edge(2,4));
graph[1].push_back(Edge(2,1));
graph[1].push_back(Edge(3,7));
graph[2].push_back(Edge(4,3));
graph[3].push_back(Edge(5,1));
graph[4].push_back(Edge(5,5));
graph[4].push_back(Edge(3,2));
Edge e(0,0);
e.dijkstras(graph, 0);
}