結果
問題 | No.1065 電柱 / Pole (Easy) |
ユーザー |
![]() |
提出日時 | 2020-05-29 21:28:56 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 297 ms / 2,000 ms |
コード長 | 2,369 bytes |
コンパイル時間 | 3,331 ms |
コンパイル使用メモリ | 210,248 KB |
最終ジャッジ日時 | 2025-01-10 16:31:36 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 46 |
ソースコード
#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'; }