-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra-std.cpp
More file actions
41 lines (41 loc) · 807 Bytes
/
Dijkstra-std.cpp
File metadata and controls
41 lines (41 loc) · 807 Bytes
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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int dis;
int pos;
bool operator>(const node &x) const
{
return x.dis > dis;
}
};
struct edge
{
int v;
int w;
};
vector<edge> connects[100001];
vector<int> dist(100001);
vector<bool> visit(100001);
priority_queue<node> q;
void priority_dijstra(int start)
{
q.push((node){0, start});
while (!q.empty())
{
int dis = q.top().dis;
int pos = q.top().pos;
q.pop();
if (visit[pos])
continue;
visit[pos] = true;
for (auto con : connects[pos])
{
if (dist[con.v] > dist[pos] + con.w)
{
dist[con.v] = dist[pos] + con.w;
q.push((node{dist[con.v],con.v}));
}
}
}
}