-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlight_Discount.cpp
More file actions
54 lines (54 loc) · 1.47 KB
/
Copy pathFlight_Discount.cpp
File metadata and controls
54 lines (54 loc) · 1.47 KB
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"
// #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll res=LLONG_MAX;
ll INF=LLONG_MAX;
vector<ll> dijkstra(ll V, vector<vector<pair<ll, ll>>> &adj, int src){
priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> pq;
vector<ll> dist(V, LLONG_MAX);
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
ll u = pq.top().second;
if(dist[u] < pq.top().first) {
pq.pop();
continue;
}
pq.pop();
for (auto it : adj[u]) {
ll v = it.first;
ll w = it.second;
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
pq.push(make_pair(dist[v], v));
}
}
}
return dist;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
cin>>n>>m;
vector<vector<pair<ll, ll>>> adj1(n), adj2(n);
for (ll i = 0; i < m; i++) {
ll x, y, z;
cin >> x >> y >> z;
adj1[x - 1].push_back(make_pair(y - 1, z));
adj2[y - 1].push_back(make_pair(x - 1, z));
}
ll res=LLONG_MAX;
vector<ll> dist1 = dijkstra(n, adj1, 0);
vector<ll> dist2 = dijkstra(n, adj2, n-1);
for (ll u = 0; u < n; u++) {
for (auto [v, w] : adj1[u]) {
if (dist1[u] != INF && dist2[v] != INF) {
res = min(res, w / 2 + dist1[u] + dist2[v]);
}
}
}
cout<<res<<endl;
return 0;
}