Advertisement
_rashed

SPOJ MELE3

Jan 26th, 2023
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /*
  6. Ordered set usage:
  7. order_of_key (k) : Number of items strictly smaller than k.
  8. find_by_order(k) : K-th element in a set (counting from zero).
  9. */
  10.  
  11. #include <ext/pb_ds/assoc_container.hpp>
  12. #include <ext/pb_ds/tree_policy.hpp>
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  15.  
  16. const int OO = 1e9;
  17. const double EPS = 1e-9;
  18.  
  19. vector<vector<int>> graph;
  20. int best[1001];
  21. int k,n;
  22.  
  23. int main()
  24. {
  25.     ios_base::sync_with_stdio(NULL);
  26.     cin.tie(0);
  27.     cin >> k >> n;
  28.     graph.resize(k+1);
  29.     for(int i = 1; i <= k; i++) {
  30.         best[i] = -1;
  31.     }
  32.     for(int i = 0; i < n; i++) {
  33.         int a,b;
  34.         cin >> a >> b;
  35.         graph[a].push_back(b);
  36.         graph[b].push_back(a);
  37.     }
  38.     priority_queue<pair<int,int>> pq;
  39.     pq.push({0,1});
  40.     while(!pq.empty()) {
  41.         pair<int,int> curr = pq.top();
  42.         pq.pop();
  43.         curr.first *= -1;
  44.         if(best[curr.second] != -1) {
  45.             continue;
  46.         }
  47.         best[curr.second] = curr.first;
  48.         for(int c : graph[curr.second]) {
  49.             if(best[c] == -1) {
  50.                 int high = max(c,curr.second);
  51.                 int low = min(c,curr.second);
  52.                 int f_num = high-low+1;
  53.                 int st_num = 2*f_num - 2;
  54.                 int st = curr.first%st_num;
  55.                 bool d = st >= st_num/2;
  56.                 int floor = (d ? high-(st-st_num/2): st+low);
  57.                 int time = curr.first + high-low;
  58.                 if(floor > curr.second && d == 0) {
  59.                     time += high-floor + high-curr.second;
  60.                 }
  61.                 else if(floor < curr.second && d == 1) {
  62.                     time += floor-low + curr.second-low;
  63.                 }
  64.                 else {
  65.                     time += abs(curr.second-floor);
  66.                 }
  67.                 //cout << " at curr.second = " << curr.second << " c = " << c << " floor = " << floor << " d = " << d << " time = " << time << "\n";
  68.                 pq.push({-1*time,c});
  69.             }
  70.         }
  71.     }
  72.     /*for(int i = 1; i <= k; i++) {
  73.         cout << "best of " << i << " is " << best[i] << "\n";
  74.     }*/
  75.     cout << best[k]*5 << "\n";
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement