forked from iamvickynguyen/Kattis-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bryr.cpp
45 lines (39 loc) · 1.04 KB
/
bryr.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
#include <iostream>
#include <utility>
#include <vector>
#include <set>
using namespace std;
const int INF = 200001;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, a, b, c;
cin >> n >> m;
vector<vector<pair<int, int>>> graph(n);
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
a--; b--;
graph[a].push_back(make_pair(b, c));
graph[b].push_back(make_pair(a, c));
}
vector<int> dist(n, INF);
dist[0] = 0;
set<pair<int, int>> pq;
for (int i = 0; i < n; i++)
pq.emplace(dist[i], i);
while (!pq.empty()) {
auto front = *pq.begin();
int d = front.first, cur = front.second;
pq.erase(pq.begin());
for (auto item : graph[cur]) {
int v = item.first, w = item.second;
if (dist[cur] + w < dist[v]) {
pq.erase(pq.find({dist[v], v}));
dist[v] = dist[cur] + w;
pq.emplace(dist[v], v);
}
}
}
cout << dist[n - 1];
return 0;
}