-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijakstraSingleSourceShortestPath.cpp
More file actions
76 lines (62 loc) · 1.61 KB
/
dijakstraSingleSourceShortestPath.cpp
File metadata and controls
76 lines (62 loc) · 1.61 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
#define INF 100000
using namespace std;
/*
int dijakstra(vector<pair<int,int> >edge[],int source,int destination,int n){
vector<int>cost(n+1,INF);
cost[source]=0;
queue<pair<int,int> >Q;
vector<bool>visit(n+1,false);
Q.push({source,0});
while(!Q.empty()){
int temp1 = Q.front().first;
int weight = Q.front().second;
visit[temp1]=true;
Q.pop();
for(auto it:edge[temp1]){
int temp = it.first;
int weight = it.second;
cost[temp]=min(cost[temp],cost[temp1]+weight);
if(!visit[temp]) Q.push({temp,weight});
}
}
return cost[destination];
} */
int dijakstra(vector<pair<int,int> >edge[],int source,int destination,int n){
vector<int>cost(n+1,INF);
cost[source]=0;
priority_queue<pair<int,int> >Q;
Q.push({0,source});
while(!Q.empty()){
int weight = Q.top().first;
int node = Q.top().second;
Q.pop();
for(auto it:edge[node]){
int temp = it.first;
int weight = it.second;
if(cost[temp]>cost[node]+weight){
cost[temp]=cost[node]+weight;
Q.push({-cost[temp],temp});
}
}
}
return cost[destination];
}
int main(){
int n,m;
printf("Give the number of node and edge: ");
scanf("%d %d",&n,&m);
vector<pair<int,int> >edge[n+5];
printf("Give the edges and cost: ");
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
edge[u].push_back({v,w});
edge[v].push_back({u,w});
}
int source,destination;
printf("Give the source node and destination node: ");
scanf("%d %d",&source,&destination);
printf("Shortest distance from %d to %d is %d\n",source,destination,dijakstra(edge,source,destination,n));
return 0;
}