結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー merom686
提出日時 2020-05-29 22:04:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 387 ms / 2,000 ms
コード長 1,861 bytes
コンパイル時間 1,589 ms
コンパイル使用メモリ 108,028 KB
最終ジャッジ日時 2025-01-10 17:19:05
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct Graph {
    static constexpr double INF = 1e+100;
    struct VertexQ {
        bool operator<(const VertexQ &o) const {
            return c > o.c; // 逆
        }
        int i;
        double c;
    };
    struct Vertex { int n; double c; };
    struct Edge { int i, n; double c; };
    Graph(int n, int m) : v(n, { -1, INF }), e(m), n(n), m(0) {}
    void add_edge(int i, int j, double c) {
        e[m] = { j, v[i].n, c };
        v[i].n = m;
        m++;
    }
    void dijkstra(int i, int j) {
        for (int i = 0; i < n; i++) v[i].c = INF;
        priority_queue<VertexQ> q;
        q.push({ i, v[i].c = 0 });
        while (!q.empty()) {
            auto p = q.top(); q.pop();
            if (p.i == j) break;
            if (p.c > v[p.i].c) continue;
            for (int j = v[p.i].n; j >= 0; j = e[j].n) {
                Edge &o = e[j];
                double c = p.c + o.c;
                if (c < v[o.i].c) q.push({ o.i, v[o.i].c = c });
            }
        }
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
};

int main() {
    int n, m;
    cin >> n >> m;
    
    Graph g(n, m * 2);

    int x, y;
    cin >> x >> y;
    x--; y--;

    vector<pair<int, int>> p(n);
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        p[i] = { x, y };
    }

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;

        double c = hypot(p[a].first - p[b].first, p[a].second - p[b].second);
        g.add_edge(a, b, c);
        g.add_edge(b, a, c);
    }

    g.dijkstra(x, y);

    cout << fixed << setprecision(9) << g.v[y].c << '\n';

    return 0;
}
0