Advertisement
cosenza987

Untitled

Feb 12th, 2023
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. const int N = 1e5 + 7;
  8.  
  9. struct line {
  10.     long long b, m;
  11. };
  12.  
  13. int nh, pos;
  14. line hull[N], v[N];
  15. pair<long long, int> tmp[N];
  16.  
  17. bool check(line s, line t, line u) {
  18.     return (s.b - t.b) * (u.m - s.m) < (s.b - u.b) * (t.m - s.m);
  19. }
  20.  
  21. void update(line s) {
  22.     while(nh >= 2 and !check(hull[nh - 2], hull[nh - 1], s)) {
  23.         nh--;
  24.     }
  25.     hull[nh++] = s;
  26. }
  27.  
  28. long long eval(int id, long long x) {
  29.     return hull[id].b + hull[id].m * x;
  30. }
  31.  
  32. long long query(long long x) {
  33.     while(pos + 1 < nh and eval(pos, x) < eval(pos + 1, x)) {
  34.         pos++;
  35.     }
  36.     return -eval(pos, x);
  37. }
  38.  
  39. int main() {
  40.     ios_base::sync_with_stdio(false);
  41.     cin.tie(nullptr);
  42.     int n, m, s, t;
  43.     cin >> n >> m >> s >> t;
  44.     vector<long long> h(n + 1);
  45.     vector<vector<int>> adj(n + 1);
  46.     for(int i = 1; i <= n; i++) {
  47.         cin >> h[i];
  48.         h[i] <<= 1;
  49.     }
  50.     bool foi = false;
  51.     for(int i = 0; i < m; i++) {
  52.         int a, b;
  53.         cin >> a >> b;
  54.         adj[a].push_back(b);
  55.         adj[b].push_back(a);
  56.         if((a == s and b == t) or (a == t and b == s)) {
  57.             foi = true;
  58.         }
  59.     }
  60.     if(foi) {
  61.         cout << 0 << "\n";
  62.         return 0;
  63.     }
  64.     vector<vector<long long>> dist(2, vector<long long>(n + 1, LLONG_MAX));
  65.     priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
  66.     dist[0][s] = dist[1][t] = 0;
  67.     pq.push({0, s});
  68.     while(!pq.empty()) {
  69.         auto p = pq.top(); pq.pop();
  70.         for(auto e : adj[p.second]) {
  71.             if(dist[0][e] > p.first + (h[e] - h[p.second]) * (h[e] - h[p.second])) {
  72.                 dist[0][e] = p.first + (h[e] - h[p.second]) * (h[e] - h[p.second]);
  73.                 pq.push({dist[0][e], e});
  74.             }
  75.         }
  76.     }
  77.     pq.push({0, t});
  78.     while(!pq.empty()) {
  79.         auto p = pq.top(); pq.pop();
  80.         for(auto e : adj[p.second]) {
  81.             if(dist[1][e] > p.first + (h[e] - h[p.second]) * (h[e] - h[p.second])) {
  82.                 dist[1][e] = p.first + (h[e] - h[p.second]) * (h[e] - h[p.second]);
  83.                 pq.push({dist[1][e], e});
  84.             }
  85.         }
  86.     }
  87.     long long ans = LLONG_MAX;
  88.     int sz = 0;
  89.     for(int i = 1; i <= n; i++) {
  90.         if(adj[i].size() == 1 or i == s or i == t) {
  91.             continue;
  92.         }
  93.         nh = pos = sz = 0;
  94.         for(auto e : adj[i]) {
  95.             v[sz] = {-h[e] * h[e] / 2 - dist[0][e], h[e]};
  96.             tmp[sz++] = {h[e], e};
  97.         }
  98.         sort(v, v + sz, [](line s, line t) {
  99.             return (s.m == t.m) ? (s.b < t.b) : (s.m < t.m);
  100.         });
  101.         sort(tmp, tmp + sz);
  102.         for(int i = 0; i < sz; i++) {
  103.             update(v[i]);
  104.         }
  105.         for(int i = 0; i < sz; i++) {
  106.             long long cur = query(tmp[i].first) + dist[1][tmp[i].second] + tmp[i].first * tmp[i].first / 2;
  107.             ans = min(ans, cur);
  108.         }
  109.     }
  110.     cout << ans / 4 << (ans % 4 == 2 ? ".5\n" : "\n");
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement