-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflight_discount.cpp
54 lines (50 loc) · 1.2 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
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define endl "\n"
using namespace std;
typedef pair<int, int> pii;
int N, M;
vector<pii> graph[100000], rgraph[100000];
priority_queue<pii, vector<pii>, greater<pii>> pq;
int di[100000], di2[100000];
void dij(int s){
fill(di, di+N, 1e17);
pq.push({0, s});
di[s] = 0;
while(!pq.empty()){
int id = pq.top().se, d = pq.top().fi;
pq.pop();
if(di[id] != d) continue;
for(pii i : graph[id]){
if(di[i.fi] > di[id] + i.se){
di[i.fi] = di[id] + i.se;
pq.push({di[i.fi], i.fi});
}
}
}
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for(int i = 0; i < M; i++){
int a, b, c; cin >> a >> b >> c;
graph[a-1].push_back({b-1, c});
rgraph[b-1].push_back({a-1, c});
}
dij(0);
copy(di, di+N, di2);
for(int i = 0; i < N; i++){
graph[i] = rgraph[i];
}
dij(N-1);
int ans = 1e17;
for(int i = 0; i < N; i++){
for(pii j : graph[i]){
ans = min(ans, di[i] + di2[j.fi] + j.se / 2);
}
}
cout << ans << endl;
}