結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー MisterMister
提出日時 2020-08-02 14:42:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 2,472 bytes
コンパイル時間 1,241 ms
コンパイル使用メモリ 112,244 KB
実行使用メモリ 44,036 KB
最終ジャッジ日時 2023-09-25 17:06:50
合計ジャッジ時間 13,217 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 147 ms
19,040 KB
testcase_03 AC 195 ms
41,664 KB
testcase_04 AC 193 ms
41,548 KB
testcase_05 AC 141 ms
44,036 KB
testcase_06 AC 141 ms
41,272 KB
testcase_07 AC 46 ms
10,912 KB
testcase_08 AC 167 ms
32,472 KB
testcase_09 AC 16 ms
5,820 KB
testcase_10 AC 76 ms
16,104 KB
testcase_11 AC 53 ms
12,264 KB
testcase_12 AC 29 ms
10,596 KB
testcase_13 AC 162 ms
23,292 KB
testcase_14 AC 191 ms
29,276 KB
testcase_15 AC 225 ms
31,660 KB
testcase_16 AC 92 ms
17,648 KB
testcase_17 AC 246 ms
34,328 KB
testcase_18 AC 63 ms
13,692 KB
testcase_19 AC 230 ms
30,504 KB
testcase_20 AC 54 ms
10,724 KB
testcase_21 AC 75 ms
16,676 KB
testcase_22 AC 206 ms
28,476 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 27 ms
10,352 KB
testcase_26 AC 120 ms
21,336 KB
testcase_27 AC 115 ms
19,024 KB
testcase_28 AC 213 ms
31,508 KB
testcase_29 AC 19 ms
7,712 KB
testcase_30 AC 232 ms
34,096 KB
testcase_31 AC 155 ms
29,556 KB
testcase_32 AC 100 ms
18,856 KB
testcase_33 AC 222 ms
35,988 KB
testcase_34 AC 68 ms
14,032 KB
testcase_35 AC 239 ms
34,328 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 4 ms
4,380 KB
testcase_38 AC 3 ms
4,384 KB
testcase_39 AC 4 ms
4,384 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 341 ms
33,872 KB
testcase_42 AC 86 ms
12,692 KB
testcase_43 AC 160 ms
19,672 KB
testcase_44 AC 47 ms
9,352 KB
testcase_45 AC 151 ms
19,256 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <tuple>
#include <queue>
#include <limits>

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

template <class Cost>
std::vector<Cost> dijkstra(const Graph<Cost>& graph, int s) {
    constexpr Cost INF = std::numeric_limits<Cost>::max();

    std::vector<Cost> dist(graph.size(), INF);
    dist[s] = 0;
    MinHeap<std::pair<Cost, int>> que;
    que.emplace(0, s);

    while (!que.empty()) {
        int v;
        Cost d;
        std::tie(d, v) = que.top();
        que.pop();
        if (d > dist[v]) continue;

        for (const auto& e : graph[v]) {
            if (dist[e.dst] <= dist[v] + e.cost) continue;
            dist[e.dst] = dist[v] + e.cost;
            que.emplace(dist[e.dst], e.dst);
        }
    }

    return dist;
}

using ldouble = long double;

void solve() {
    int n, m, s, t;
    std::cin >> n >> m >> s >> t;
    --s, --t;

    std::vector<std::pair<ldouble, ldouble>> ps(n);
    for (auto& p : ps) std::cin >> p.first >> p.second;

    Graph<ldouble> graph(n);
    while (m--) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;

        auto c = std::hypot(ps[u].first - ps[v].first, ps[u].second - ps[v].second);
        graph.span(false, u, v, c);
    }

    auto ds = dijkstra(graph, s);
    std::cout << std::fixed << std::setprecision(10)
              << ds[t] << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0