fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4. #define ull unsigned long long
  5. #define mod 1000000007
  6. #define pb push_back
  7. #define ll long long
  8. const ll INF = 1e18;
  9. ll n, m, k;
  10. ll d(ll w, const vector<vector<pair<ll, pair<ll, ll>>>>& adj) {
  11. priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  12. vector<ll> dis(n + 1, INF);
  13. dis[1] = 0;
  14. pq.push({0, 1});
  15. while (!pq.empty()) {
  16. ll cost = pq.top().first;
  17. ll node = pq.top().second;
  18. pq.pop();
  19. if (cost > dis[node]) continue;
  20. for (auto& edge : adj[node]) {
  21. ll neigh = edge.first;
  22. ll c = edge.second.first;
  23. ll weight = edge.second.second;
  24. if (weight > w) continue;
  25. if (dis[neigh] > cost + c) {
  26. dis[neigh] = cost + c;
  27. pq.push({dis[neigh], neigh});
  28. }
  29. }
  30. }
  31. return dis[n];
  32. }
  33. int main() {
  34. ios::sync_with_stdio(0);
  35. cin.tie(0);
  36. cout.tie(0);
  37. int t;
  38. cin >> t;
  39. while (t--) {
  40. cin >> n >> m >> k;
  41. vector<vector<pair<ll, pair<ll, ll>>>> adj(n + 1);
  42. for (int i = 0; i < m; i++) {
  43. ll from, to, c, w;
  44. cin >> from >> to >> c >> w;
  45. adj[from].pb({to, {c, w}});
  46. adj[to].pb({from, {c, w}});
  47. }
  48. if (n == 1) {
  49. cout << "0" << endl;
  50. continue;
  51. }
  52. if (m == 0) {
  53. cout << "-1" << endl;
  54. continue;
  55. }
  56. ll l = 0, r = 2e9, anss = -1;
  57. while (l <= r) {
  58. ll mid = l + (r - l) / 2;
  59. ll cost = d(mid, adj);
  60. if (cost < k) {
  61. anss = mid;
  62. r = mid - 1;
  63. } else {
  64. l = mid + 1;
  65. }
  66. }
  67. cout << anss << endl;
  68. }
  69. }
  70.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty