-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
54 lines (51 loc) · 1.06 KB
/
Dijkstra.cpp
File metadata and controls
54 lines (51 loc) · 1.06 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
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define LD long double
#define gc getchar_unlocked
#define pc putchar_unlocked
#define MOD 1000000007
#define MAXN 100005
#define bitcount _builtin_popcount
#define INF 2000000000
#define EPS 1e-9
#define PI 3.14159265359
template<typename T>T absll(T X)
{
if(X<0)
return -1*X;
else
return X;
}
int main()
{
vector<LL> dist(N+2,INF);
priority_queue<pair<LL,LL>,vector<pair<LL,LL> >,greater<pair<LL,LL> > > PQ;
//Dijkstra Algorithm
dist[M[sourcename]]=0LL;
PQ.push(mp(0LL,M[sourcename]));
while(!PQ.empty())
{
pair<LL,LL> top=PQ.top();
PQ.pop();
LL V=top.second;
LL D=top.first;
if(D<=dist[V])
{
vector<pair<LL,LL> >::iterator it;
for(it=G[V].begin();it!=G[V].end();it++)
{
LL reachablevertex=it->first;
LL cost=it->second;
if(dist[reachablevertex]>dist[V]+cost)
{
dist[reachablevertex]=dist[V]+cost;
PQ.push(mp(dist[reachablevertex],reachablevertex));
}
}
}
}
}