Advertisement
aayyk

Untitled

Mar 24th, 2022
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. /*
  2.    Dear God, make it alright
  3.    Only You can make it alright
  4.    Dear Lord, make it alright
  5.    Nothing else ever feels right
  6. */
  7.  
  8. //#pragma GCC optimize("Ofast,unroll-loops")
  9. #include<algorithm>
  10. #include<fstream>
  11. #include<iostream>
  12. #include<set>
  13. #include<forward_list>
  14. #include<vector>
  15. #include<tuple>
  16.  
  17. using namespace std;
  18.  
  19. //#define int ll
  20.  
  21. typedef long long ll;
  22. typedef long double ld;
  23. #define sz(x) int((x).size())
  24.  
  25. template<class T>
  26. T ScanInt(FILE* input) {
  27.     int c = 0;
  28.     T x = 0;
  29.     for(; c < '0' || c > '9'; c = fgetc(input));
  30.     for(; c >= '0' && c <= '9'; c = fgetc(input)) {
  31.         x = (x << 1) + (x << 3) + c - '0';
  32.     }
  33.     return x;
  34. }
  35.  
  36. void ToStartFile(FILE* fptr) {
  37.     fseek(fptr, 0, SEEK_SET);
  38. }
  39.  
  40. const int N = 2e5;
  41.  
  42. struct Tree {
  43.     pair<int, int> t[4 * N];
  44.  
  45.     Tree() {
  46.         fill(t, t + 4 * N, make_pair(2e9, 0));
  47.     }
  48.  
  49.     void update(int v, int l, int r, int pos, int val) {
  50.         if (l == r) {
  51.             t[v] = { val, pos };
  52.         } else {
  53.             int m = (l + r) / 2;
  54.  
  55.             if (pos <= m) {
  56.                 update(2 * v, l, m, pos, val);
  57.             } else {
  58.                 update(2 * v + 1, m + 1, r, pos, val);
  59.             }
  60.  
  61.             t[v] = min(t[2 * v], t[2 * v + 1]);
  62.         }
  63.     }
  64.  
  65.     pair<int, int> get() {
  66.         return t[1];
  67.     }
  68.  
  69.     void set(int pos, int val) {
  70.         update(1, 0, N - 1, pos, val);
  71.     }
  72. } tree;
  73.  
  74.  
  75. int n, m;
  76. int d[N], p[N];
  77.  
  78. forward_list<pair<int, int>> g[N];
  79.  
  80. signed main() {
  81.     freopen("output.txt", "w", stdout);
  82.  
  83.     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  84.  
  85.     const int MAX_EDGES = 5e5;
  86.  
  87.     FILE* input = fopen("input.txt", "r");
  88.     n = ScanInt<int>(input);
  89.     m = ScanInt<int>(input);
  90.     int from, to, weight;
  91.  
  92.     array<int, 1002> cnt;
  93.     fill(cnt.begin(), cnt.end(), 0);
  94.  
  95.     for (int i = 0; i < m; ++i) {
  96.         from = ScanInt<int>(input);
  97.         to = ScanInt<int>(input);
  98.         weight = ScanInt<int>(input);
  99.        
  100.         cout << from << " " << to << " " << weight << " " << endl;
  101.  
  102.         --from, --to;
  103.         ++cnt[weight];
  104.     }
  105.  
  106.     cout << endl;
  107.  
  108.     int lim = 0, pref = 0;
  109.     while (lim <= 1000) {
  110.         pref += cnt[lim];
  111.         if (pref > MAX_EDGES) {
  112.             --lim;
  113.             break;
  114.         } else {
  115.             ++lim;
  116.         }
  117.     }
  118.  
  119.     ToStartFile(input);
  120.  
  121.     for (int i = 0; i < m; ++i) {
  122.         from = ScanInt<int>(input);
  123.         to = ScanInt<int>(input);
  124.         weight = ScanInt<int>(input);
  125.  
  126.         cout << from << " " << to << " " << weight << " " << endl;
  127.  
  128.         --from, --to;
  129.         if (weight <= lim) {
  130.             g[from].push_front({ to, weight });
  131.             g[to].push_front({ from, weight });
  132.         }
  133.     }
  134.  
  135.     fclose(input);
  136.  
  137.     fill(d, d + N, 2e9);
  138.     d[0] = 0;
  139.     tree.set(0, 0);
  140.  
  141.     while (true) {
  142.         auto [dist, u] = tree.get();
  143.         if (dist == 2e9) break;
  144.  
  145.         tree.set(u, 2e9);
  146.  
  147.         while (!g[u].empty()) {
  148.             auto [v, w] = *g[u].begin();
  149.             g[u].pop_front();
  150.                
  151.             cout << u << " " << v << " " << w << endl;
  152.  
  153.             if (d[v] > d[u] + w) {
  154.                 d[v] = d[u] + w;
  155.                 p[v] = u;
  156.                 tree.set(v, d[v]);
  157.  
  158.  
  159.                 /*
  160.                 while (s.size() >= MAX_EDGES) {
  161.                     s.erase(--s.end());
  162.                 }
  163.                 */
  164.  
  165.                 //cout << u << " " << v << endl;
  166.             }
  167.         }
  168.     }
  169.  
  170.     vector<int> ans;
  171.     for (int u = n - 1; u; u = p[u]) {
  172.         ans.push_back(u);
  173.     }
  174.  
  175.     ans.push_back(0);
  176.     reverse(ans.begin(), ans.end());
  177.  
  178.     cout << ans.size() << "\n";
  179.     for (int x : ans) {
  180.         cout << x + 1 << " ";
  181.     }
  182.  
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement