-
Notifications
You must be signed in to change notification settings - Fork 169
/
Flight Discount.cpp
55 lines (47 loc) · 1.31 KB
/
Flight Discount.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,ll> edge;
typedef tuple<ll,int,int> node;
const int maxN = 2e5+1;
int N, M, a, b;
ll c, dist[2][maxN];
vector<edge> G[maxN];
priority_queue<node, vector<node>, greater<node>> Q;
int main(){
scanf("%d %d", &N, &M);
memset(dist, 0x3f, sizeof(dist));
for(int i = 0; i < M; i++){
scanf("%d %d %lld", &a, &b, &c);
G[a].push_back({b, c});
}
dist[0][1] = dist[1][1] = 0;
Q.push({0, 1, 1});
while(!Q.empty()){
ll d = get<0>(Q.top());
int u = get<1>(Q.top());
int coupon = get<2>(Q.top());
Q.pop();
if(dist[!coupon][u] < d) continue;
for(edge e : G[u]){
int v = e.first;
ll w = e.second;
if(coupon){
if(dist[0][v] > d + w){
dist[0][v] = d + w;
Q.push({d+w, v, 1});
}
if(dist[1][v] > d + w/2){
dist[1][v] = d + w/2;
Q.push({d+w/2, v, 0});
}
} else {
if(dist[1][v] > d + w){
dist[1][v] = d + w;
Q.push({d+w, v, 0});
}
}
}
}
printf("%lld\n", min(dist[0][N], dist[1][N]));
}