-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlight Discount.cpp
More file actions
64 lines (48 loc) · 1.71 KB
/
Copy pathFlight Discount.cpp
File metadata and controls
64 lines (48 loc) · 1.71 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
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct datos {
long long node, cost, coupon_used;
bool operator>(const datos &other) const {
return cost > other.cost;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin >> n >> m;
vector<vector<pair<long long, long long>>> adj(n + 1);
for (long long i = 0; i < m; i++) {
long long a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b, c});
}
vector<vector<long long>> dist(n + 1, vector<long long>(2, INF));
priority_queue<datos, vector<datos>, greater<datos>> pq;
dist[1][0] = 0;
pq.push({1, 0, 0});
while (!pq.empty()) {
datos cur = pq.top();
pq.pop();
long long node = cur.node, cost = cur.cost, coupon_used = cur.coupon_used;
if (cost > dist[node][coupon_used]) continue;
for (auto &edge : adj[node]) {
long long next_node = edge.first;
long long flight_cost = edge.second;
if (dist[next_node][coupon_used] > dist[node][coupon_used] + flight_cost) {
dist[next_node][coupon_used] = dist[node][coupon_used] + flight_cost;
pq.push({next_node, dist[next_node][coupon_used], coupon_used});
}
if (!coupon_used) {
long long discounted_cost = flight_cost / 2;
if (dist[next_node][1] > dist[node][0] + discounted_cost) {
dist[next_node][1] = dist[node][0] + discounted_cost;
pq.push({next_node, dist[next_node][1], 1});
}
}
}
}
cout << min(dist[n][0], dist[n][1]) << "\n";
return 0;
}