-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA. Mania de Par.cpp
60 lines (46 loc) · 968 Bytes
/
A. Mania de Par.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1'0'000;
const ll OO = 1'000'000'000;
int N, M;
vector<pair<ll,int>> ADJ[MAXN];
ll dist[2][MAXN];
void dijkstra() {
using state = tuple<ll, int, int>;
priority_queue<state, vector<state>, greater<state>> pq;
dist[0][0] = 0;
pq.emplace(0, 0, 0);
while (!pq.empty()) {
auto [du, qu, u] = pq.top();
pq.pop();
if (dist[qu][u] < du)
continue;
for (auto [w, v] : ADJ[u]) {
ll w2 = w + du;
if (dist[!qu][v] > w2) {
dist[!qu][v] = w2;
pq.emplace(w2, !qu, v);
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 0; i < MAXN; i++) {
dist[0][i] = dist[1][i] = OO;
}
cin >> N >> M;
while (M--) {
int u, v;
ll w;
cin >> u >> v >> w;
u--, v--;
ADJ[u].emplace_back(w, v);
ADJ[v].emplace_back(w, u);
}
dijkstra();
cout << (dist[0][N-1] == OO ? -1 : dist[0][N-1]) << '\n';
}
// AC, graphs, dijkstra