题解 | #游游出游#
游游出游
https://www.nowcoder.com/practice/e787b99f04c1498aa32b9430a4616d8a
而对于每个重量而言,能走的边是固定的,所以能不能到达 n 也是确定的,那么如果存在最终答案的重量 w ,所有大于等于 w 的重量一定也能到达 n ,而所有小于 w 的都不可能到达,所以我们可以二分
然后判断一个重量能不能到达就直接跑dijk
#include <bits/stdc++.h> using namespace std; #define int long long const int INF = 1e18; const int N = 2e5 + 5; int __t = 1, n; vector<pair<int, pair<int, int>>> adj[N]; int dist[N]; bool dijkstra(int n, int h, int max_weight) { fill(dist, dist + N, INF); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; dist[1] = 0; pq.push({0, 1}); while (!pq.empty()) { int d = pq.top().first; int u = pq.top().second; pq.pop(); if (d > dist[u]) continue; for (auto e : adj[u]) { int v = e.first; int w = e.second.first; int len = e.second.second; if (w >= max_weight && dist[u] + len < dist[v]) { dist[v] = dist[u] + len; pq.push({dist[v], v}); } } } return dist[n] <= h; } void solve() { int n, m, h; cin >> n >> m >> h; for (int i = 0; i < m; ++i) { int u, v, w, d; cin >> u >> v >> w >> d; adj[u].emplace_back(v, make_pair(w, d)); adj[v].emplace_back(u, make_pair(w, d)); } int l = 1, r = 1e9, ans = -1; while (l <= r) { int mid = (l + r) / 2; if (dijkstra(n, h, mid)) { ans = mid; l = mid + 1; } else { r = mid - 1; } } cout << ans << "\n"; } int32_t main() { #ifdef ONLINE_JUDGE ios::sync_with_stdio(false); cin.tie(0); #endif // cin >> __t; while (__t--) solve(); return 0; }