結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2021-02-27 16:37:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 3,916 bytes
コンパイル時間 1,166 ms
コンパイル使用メモリ 99,072 KB
実行使用メモリ 31,628 KB
最終ジャッジ日時 2024-04-10 15:43:15
合計ジャッジ時間 4,251 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 35 ms
18,164 KB
testcase_03 AC 45 ms
31,628 KB
testcase_04 AC 47 ms
27,852 KB
testcase_05 AC 37 ms
27,772 KB
testcase_06 AC 34 ms
29,216 KB
testcase_07 AC 16 ms
11,092 KB
testcase_08 AC 51 ms
28,000 KB
testcase_09 AC 6 ms
6,988 KB
testcase_10 AC 26 ms
15,148 KB
testcase_11 AC 18 ms
13,140 KB
testcase_12 AC 6 ms
9,752 KB
testcase_13 AC 30 ms
18,164 KB
testcase_14 AC 32 ms
19,284 KB
testcase_15 AC 48 ms
27,540 KB
testcase_16 AC 24 ms
14,924 KB
testcase_17 AC 49 ms
26,364 KB
testcase_18 AC 19 ms
12,452 KB
testcase_19 AC 46 ms
24,184 KB
testcase_20 AC 15 ms
11,128 KB
testcase_21 AC 21 ms
13,948 KB
testcase_22 AC 39 ms
20,932 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 7 ms
10,964 KB
testcase_26 AC 23 ms
15,496 KB
testcase_27 AC 23 ms
15,112 KB
testcase_28 AC 40 ms
20,872 KB
testcase_29 AC 6 ms
8,156 KB
testcase_30 AC 42 ms
23,856 KB
testcase_31 AC 30 ms
19,312 KB
testcase_32 AC 19 ms
14,944 KB
testcase_33 AC 41 ms
24,292 KB
testcase_34 AC 19 ms
12,156 KB
testcase_35 AC 46 ms
24,100 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 3 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 128 ms
28,872 KB
testcase_42 AC 25 ms
12,344 KB
testcase_43 AC 39 ms
18,108 KB
testcase_44 AC 13 ms
9,712 KB
testcase_45 AC 49 ms
17,748 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <unistd.h>

struct IO {
  static const int bufsize=1<<24;
  char ibuf[bufsize], obuf[bufsize];
  char *ip, *op;
  IO(): ip(ibuf), op(obuf) { for(int t = 0, k = 0; (k = read(STDIN_FILENO, ibuf+t, sizeof(ibuf)-t)) > 0; t+=k); }
  ~IO(){ for(int t = 0, k = 0; (k = write(STDOUT_FILENO, obuf+t, op-obuf-t)) > 0; t+=k); }
  long long scan_int(){
    long long x=0;
    bool neg=false;
    for(;*ip<'+';ip++) ;
    if(*ip=='-'){ neg=true; ip++;}
    else if(*ip=='+') ip++;
    for(;*ip>='0';ip++) x = 10*x+*ip-'0';
    if(neg) x = -x;
    return x;
  }
  void put_int(long long x, char c=0){
    static char tmp[20];
    if(x==0) *op++ = '0';
    else {
      int i;
      if(x<0){
        *op++ = '-';
        x = -x;
      }
      for(i=0; x; i++){
        tmp[i] = x % 10;
        x /= 10;
      }
      for(i--; i>=0; i--)
        *op++ = tmp[i]+'0';
    }
    if(c) *op++ = c;
  }
  void put_double(double x, char c=0){
    unsigned y;
    const int mask = (1<<24) - 1;
    put_int(x);
    *op++ = '.';
    x = x - (int) x;
    if(x < 0) x = -x;
    y = x * (1<<24);

    for(int i=0;i<7;i++){
      y *= 10;
      *op++ = '0' + (y>>24);
      y &= mask;
    }
    if(c) *op++ = c;
  }
  inline char scan_char(){ return *ip++; }
  inline void put_char(char c){ *op++ = c; }
  inline char *scan_string(){ char *s = ip; while(*ip!='\n'&&*ip!=' ') ip++; *ip++='\0'; return s;}
  inline void put_string(const char *s, char c=0){ while(*s) *op++=*s++; if(c) *op++=c;}
} io;

using s32 = int;
using u32 = unsigned int;
using s64 = long long int;
using u64 = unsigned long long int;

template <typename T>
class RadixHeap {
  private:
    using P = std::pair<u64, T>;
    u64 last;
    int size;
    std::vector<P> A[65];
    int clz(u64 x){
      if(x==0) return 64;
      else return __builtin_clzll(x);
    }
  public:
    RadixHeap(): last(0), size(0) {}
    bool empty() const { return size == 0; }
    void push(u64 a, const T &b){
      A[clz(last^a)].emplace_back(P(a, b));
      size++;
    }
    P pop() {
      if(A[64].empty()){
        int i;
        for(i=63;i>=0 && A[i].empty();i--) ;
        last = std::min_element(A[i].begin(), A[i].end(), [](P &x, P &y){ return x.first < y.first; })->first;
        for(auto it = A[i].cbegin(); it != A[i].cend(); ++it){
          A[clz(last^it->first)].push_back(*it);
        }
        A[i].clear();
      }
      P x = A[64].back();
      A[64].pop_back();
      size--;
      return x;
    }
};


u64 norm(s32 x, s32 y){
  return std::sqrt((s64) x * x + (s64) y * y) * (1ULL << 32);
}

int n, m, x, y;
std::vector<std::vector<std::pair<int, u64> > > edges;
std::vector<std::pair<s32, s32> > pq;
std::vector<u64> lb;

int main(){
  n = io.scan_int();
  m = io.scan_int();
  x = io.scan_int()-1;
  y = io.scan_int()-1;

  pq.reserve(n);
  for(int i = 0; i < n; i ++){
    s32 p = io.scan_int();
    s32 q = io.scan_int();
    pq.emplace_back(p, q);
  }

  edges.resize(n);
  for(int i = 0; i < m; i ++){
    int p = io.scan_int()-1;
    int q = io.scan_int()-1;
    u64 d = norm(pq[p].first - pq[q].first, pq[p].second - pq[q].second);
    edges[p].emplace_back(q, d);
    edges[q].emplace_back(p, d);
  }

  lb.reserve(n);
  for(int i = 0; i < n; i ++){
    lb.push_back(norm(pq[i].first - pq[y].first, pq[i].second - pq[y].second));
  }

  RadixHeap<std::pair<int, u64> > queue;
  constexpr u64 maxd = (1ULL << 63) - 1;
  std::vector<u64> dist(n, maxd);

  queue.push(lb[x], {x, 0});
  dist[x] = 0;

  while(!queue.empty()){
    auto [v, d] = queue.pop().second;
    if(dist[v] < d) continue;
    if(v == y) break;
    for(auto e: edges[v]){
      int w = e.first;
      u64 c = e.second;
      if(d + c < dist[w]){
        dist[w] = d + c;
        queue.push(d + c + lb[w], {w, d + c});
      }
    }
  }

  io.put_double((double) dist[y] / (1ULL << 32), '\n');

  return 0;
}
0