forked from ShreyaDhir/HacktoberFest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra_Algorithm.cpp
49 lines (45 loc) · 1.22 KB
/
Dijkstra_Algorithm.cpp
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
// code by yctseng1227
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct edge{
int e,w;
};
inline bool operator<(const edge &a,const edge &b){
return a.w>b.w; //Notice!
}
vector<edge> V[10000];//Graph Data
int dist[10000]; //Save the distance from S
int N,M,S,E;
int main(){
int a,b,w;
while(~scanf("%d%d",&N,&M)){
for(int i=0;i<N;++i) V[i].clear();
memset(dist,0x3f,sizeof(dist)); //init to INF
while(M--){
//Add edge(a to b with w)
scanf("%d%d%d",&a,&b,&w);
V[a].push_back((edge){b,w});
V[b].push_back((edge){a,w});
}
scanf("%d%d",&S,&E); //ask
//Algorithm:Dijkstra with Priority Queue
priority_queue<edge> pq;
pq.push((edge){S,0});
while(!pq.empty()){
edge t=pq.top();pq.pop();
if(t.e==E){
dist[t.e]=t.w;
break; //Find answer!
}
if(dist[t.e]!=0x3f3f3f3f)continue;
dist[t.e]=t.w;
for(edge &e:V[t.e])
if(dist[e.e]>dist[t.e]+e.w)
pq.push((edge){e.e,dist[t.e]+e.w});
}
printf("%d\n",dist[E]);
}
}