-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
60 lines (48 loc) · 1.47 KB
/
Dijkstra.cpp
File metadata and controls
60 lines (48 loc) · 1.47 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
#include <bits/stdc++.h>
using namespace std;
string PROBLEM_NAME = "time";
const int MAXN = 200000;
int N, M;
vector<vector<pair<int, int> > > adj;
//first is node connected to, second is weight to that node
int dist[MAXN];
int path[MAXN];
int main () {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((PROBLEM_NAME+".in").c_str(), "r", stdin);
freopen((PROBLEM_NAME+".out").c_str(), "w", stdout);
cin >> N >> M;
adj.resize(N);
fill_n(dist, N, 1);
int source = 0;
int endpoint = 1; //beginning and ending of path
dist[source] = 0;
path[source] = source;
int node1, node2, weight;
for (int i = 0; i < N; i++)
{
adj[node1].push_back(make_pair(node2, weight));
adj[node2].push_back(make_pair(node1, weight));
}
//pq will keep track of (distance, node)
priority_queue <pair <int, int>> pq; //keep track of shortest distance in play
pq.push(make_pair(0, source));
pair<int, int> el;
while (pq.empty())
{
el = pq.top(); pq.pop();
if (el.first > dist[el.second]) //if we already got to this point with a path that is more optimal, don't even relax this one's edges, because it couldn't be on the shortest path.
continue;
if (el.second == endpoint)
break;
for (pair<int, int> connection: adj[el.second])
{
if (el.first + connection.second < dist[connection.first])
{
dist[connection.first] = el.first + connection.second;
path[connection.first] = el.second;
pq.push(make_pair(dist[connection.first], connection.first));
}
}
}
}