fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fi first
  5. #define se second
  6.  
  7. template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
  8. if (a > b) {a = b; return true;} return false;
  9. }
  10. template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
  11. if (a < b) {a = b; return true;} return false;
  12. }
  13.  
  14. const long long INF = 2e18;
  15. const int N = 2e5 + 3;
  16. int n, m, D;
  17. vector<long long> d[N];
  18. vector<pair<int, int>> adj[N];
  19.  
  20. struct node {
  21. long long du; int u, j;
  22. bool operator< (const node &other) const {
  23. return du > other.du;
  24. }
  25. };
  26.  
  27. main() {
  28. ios_base::sync_with_stdio(false);
  29. cin.tie(NULL); cout.tie(NULL);
  30.  
  31.  
  32. cin >> n >> m >> D;
  33. for (int i = 1, u, v, c; i <= m; ++i) {
  34. cin >> u >> v >> c;
  35. adj[u].push_back({v, c});
  36. adj[v].push_back({u, c});
  37. }
  38. if (n == 1) {
  39. cout << 0;
  40. return 0;
  41. }
  42. for (int i = 1; i <= n; ++i)
  43. d[i].assign(adj[i].size(), INF);
  44.  
  45. priority_queue<node> pq;
  46. for (int j = 0; j < adj[1].size(); ++j) {
  47. if (mini(d[1][j], 1LL * adj[1][j].se)) {
  48. pq.push({d[1][j], 1, j});
  49. }
  50. }
  51. while (pq.size()) {
  52. long long du = pq.top().du;
  53. int v = pq.top().u;
  54. int j = pq.top().j;
  55. pq.pop();
  56. if (du != d[v][j]) continue;
  57. int u = adj[v][j].fi;
  58. int len = adj[v][j].se;
  59. if (u == n) {
  60. cout << du;
  61. return 0;
  62. }
  63. for (int j = 0; j < adj[u].size(); ++j) {
  64. if (len + D > adj[u][j].se) continue;
  65. if (mini(d[u][j], du + adj[u][j].se)) {
  66. pq.push({d[u][j], u, j});
  67. }
  68. }
  69. }
  70. cout << -1;
  71. }
  72.  
Success #stdin #stdout 0.01s 13028KB
stdin
Standard input is empty
stdout
-1