結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー kokatsu
提出日時 2022-12-14 22:53:17
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 512 ms / 2,000 ms
コード長 1,027 bytes
コンパイル時間 3,676 ms
コンパイル使用メモリ 230,908 KB
実行使用メモリ 20,328 KB
最終ジャッジ日時 2024-06-22 17:04:58
合計ジャッジ時間 16,296 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

struct Pole {
    int to;
    real dist;
}

void main() {
    int N, M, X, Y;
    readf("%d %d\n%d %d\n", N, M, X, Y);

    --X, --Y;

    auto p = new real[](N), q = new real[](N);
    foreach (i; 0 .. N) readf("%f %f\n", p[i], q[i]);

    auto poles = new int[][](N);
    foreach (_; 0 .. M) {
        int P, Q;
        readf("%d %d\n", P, Q);

        --P, --Q;
        poles[P] ~= Q, poles[Q] ~= P;
    }

    auto dists = new real[](N);
    dists[] = real.max;
    dists[X] = 0.0;

    auto heap = new BinaryHeap!(Array!Pole, "a.dist > b.dist")();
    heap.insert(Pole(X, 0.0));
    while (!heap.empty) {
        auto f = heap.front;
        heap.popFront;

        if (f.dist > dists[f.to]) continue;

        foreach (pole; poles[f.to]) {
            real d = f.dist + hypot(p[f.to]-p[pole], q[f.to]-q[pole]);
            if (d < dists[pole]) {
                dists[pole] = d;
                heap.insert(Pole(pole, d));
            }
        }
    }

    real res = dists[Y];
    writefln("%.10f", res);
}
0