-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjisktra.cpp
More file actions
41 lines (41 loc) · 720 Bytes
/
djisktra.cpp
File metadata and controls
41 lines (41 loc) · 720 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
void solve()
{
int n;
cin >> n;
std::vector<pi>adj[n+1];
for (int i = 0; i < n; ++i)
{
int c1, c2, w;
cin >> c1 >> c2 >> w;
adj[c1].pb({c2, w});
adj[c2].pb({c1, w});
}
int start;
cin >> start;
std::vector<int>visited(n+1, 0);
std::vector<ll>dist(n+1, 1e18);
// pi=pair<ll,int
priority_queue<pi, vector<pi>, greater<pi>>q;
dist[start] = 0;
q.push({0, start});
while (!q.empty())
{
ll w = q.top().first;
int city = q.top().second;
q.pop();
if (!visited[city])
{
visited[city] = 1;
for (auto it : adj[city])
{
int c2 = it.first;
ll w2 = it.second;
if (dist[city] + w2 < dist[c2])
{
dist[c2] = dist[city] + w2;
q.push({dist[c2], c2});
}
}
}
}
}