fork download
  1. #include <bits/stdc++.h>
  2. #define endl '\n'
  3. #define PRECISION 0
  4. using namespace std;
  5. using ll = long long;
  6. #define fr first
  7. #define sc second
  8. using pl2 = pair<ll, ll>;
  9.  
  10. const ll INF = 0x3f3f3f3f3f3f3f3f;
  11.  
  12. bool chk[100020];
  13. vector<pl2> adj[100020];
  14. ll dis[100020];
  15.  
  16. void Main(){
  17. int n, m; n = 100000; m = 299991; //cin >> n >> m;
  18. for (int i = 0; i < n; i++){ /*cin >> chk[i];*/ chk[i] = (i == n-1 ? 1 : 0); } chk[n-1] = 0;
  19. for (int v = 3; v <= n-2; v++){
  20. int w = 0, x = 1; // int v, w, x; cin >> v >> w >> x;
  21. adj[v].push_back({w, x}); adj[w].push_back({v, x});
  22. }
  23. for (int v = 3; v <= n-2; v++){
  24. int w = 2, x = 100000-v; // int v, w, x; cin >> v >> w >> x;
  25. adj[v].push_back({w, x}); adj[w].push_back({v, x});
  26. }
  27. for (int v = 0; v <= n-1; v++){ if (v == 1){ continue; }
  28. int w = 1, x = 100000; // int v, w, x; cin >> v >> w >> x;
  29. adj[v].push_back({w, x}); adj[w].push_back({v, x});
  30. }
  31. memset(dis, 0x3f, sizeof(dis)); dis[0] = 0;
  32. priority_queue< pl2, vector<pl2>, greater<pl2> > pq; pq.push({0, 0});
  33. while (!pq.empty()){
  34. int now = pq.top().fr; ll dst = pq.top().sc;
  35. pq.pop();
  36. if (dis[now] != dst){ continue; }
  37. for (pl2 p : adj[now]){
  38. int nxt = p.fr; ll d = p.sc;
  39. if (chk[nxt]){ continue; }
  40. if (dis[nxt] <= dst+d){ continue; }
  41. pq.push({nxt, dst+d}); dis[nxt] = dst+d;
  42. }
  43. }
  44. if (dis[n-1] == INF){ cout << -1; } else{ cout << dis[n-1]; }
  45. }
  46.  
  47. int main(){
  48. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  49. cout.setf(ios::fixed); cout.precision(PRECISION);
  50. Main();
  51. }
  52.  
Time limit exceeded #stdin #stdout 5s 21932KB
stdin
Standard input is empty
stdout
Standard output is empty