結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー risujirohrisujiroh
提出日時 2020-05-29 21:28:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 2,369 bytes
コンパイル時間 3,333 ms
コンパイル使用メモリ 211,460 KB
実行使用メモリ 22,624 KB
最終ジャッジ日時 2024-04-23 20:13:36
合計ジャッジ時間 7,043 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 82 ms
12,856 KB
testcase_03 AC 126 ms
21,912 KB
testcase_04 AC 125 ms
21,788 KB
testcase_05 AC 103 ms
21,780 KB
testcase_06 AC 100 ms
21,784 KB
testcase_07 AC 34 ms
8,668 KB
testcase_08 AC 128 ms
22,624 KB
testcase_09 AC 13 ms
5,560 KB
testcase_10 AC 57 ms
11,924 KB
testcase_11 AC 39 ms
9,460 KB
testcase_12 AC 29 ms
11,376 KB
testcase_13 AC 88 ms
17,412 KB
testcase_14 AC 105 ms
17,752 KB
testcase_15 AC 117 ms
19,392 KB
testcase_16 AC 57 ms
13,776 KB
testcase_17 AC 127 ms
21,564 KB
testcase_18 AC 42 ms
11,380 KB
testcase_19 AC 120 ms
19,348 KB
testcase_20 AC 33 ms
8,332 KB
testcase_21 AC 47 ms
13,984 KB
testcase_22 AC 119 ms
18,556 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 25 ms
10,352 KB
testcase_26 AC 65 ms
13,028 KB
testcase_27 AC 66 ms
14,324 KB
testcase_28 AC 108 ms
20,876 KB
testcase_29 AC 14 ms
6,940 KB
testcase_30 AC 120 ms
19,604 KB
testcase_31 AC 83 ms
14,456 KB
testcase_32 AC 56 ms
10,980 KB
testcase_33 AC 123 ms
18,648 KB
testcase_34 AC 43 ms
11,164 KB
testcase_35 AC 133 ms
20,452 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 4 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 174 ms
22,472 KB
testcase_42 AC 49 ms
9,344 KB
testcase_43 AC 85 ms
13,644 KB
testcase_44 AC 31 ms
7,364 KB
testcase_45 AC 83 ms
13,340 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T> struct graph {
  struct edge {
    int to;
    T w;
    operator int() const { return to; }
  };
  template <class It> struct edge_list {
    const It bg, ed;
    It begin() const { return bg; }
    It end() const { return ed; }
    auto&& operator[](size_t i) const { return bg[i]; }
    size_t size() const { return ed - bg; }
  };
  vector<pair<int, edge>> pool;
  vector<int> head;
  vector<edge> edges;
  graph(int n) : head(n + 1) {}
  void add(int from, int to, T w = 1) {
    pool.push_back({from, {to, w}});
    ++head[from];
  }
  void build() {
    partial_sum(begin(head), end(head), begin(head));
    edges.resize(head.back());
    while (not empty(pool)) {
      edges[--head[pool.back().first]] = pool.back().second;
      pool.pop_back();
    }
    decltype(pool)().swap(pool);
  }
  edge_list<class vector<edge>::const_iterator> operator[](int v) const {
    return {begin(edges) + head[v], begin(edges) + head[v + 1]};
  }
  edge_list<class vector<edge>::iterator> operator[](int v) {
    return {begin(edges) + head[v], begin(edges) + head[v + 1]};
  }
  size_t size() const { return std::size(head) - 1; }
};

template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

auto chmin = [](auto&& a, auto b) { return b < a ? a = b, 1 : 0; };

template <class T, class G> auto dijkstra(const G& g, int s) {
  vector d(size(g), inf<T>);
  vector prv(size(g), -1);
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<>> rh;
  rh.emplace(d[s] = 0, s);
  while (not empty(rh)) {
    auto [dv, v] = rh.top();
    rh.pop();
    if (dv != d[v]) continue;
    for (auto&& e : g[v])
      if (chmin(d[e.to], dv + e.w)) {
        rh.emplace(d[e.to], e.to);
        prv[e.to] = v;
      }
  }
  return make_pair(d, prv);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  --s, --t;
  vector<double> x(n), y(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i];
  }
  graph<double> g(n);
  while (m--) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    double w = hypot(x[v] - x[u], y[v] - y[u]);
    g.add(u, v, w);
    g.add(v, u, w);
  }
  g.build();
  cout << dijkstra<double>(g, s).first[t] << '\n';
}
0