結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー Moon0603
提出日時 2020-06-11 16:36:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 1,531 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 207,948 KB
最終ジャッジ日時 2025-01-11 01:12:50
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

struct Edge
{
    int to;
    double cost;
};

template<typename T>
vector<T> dijkstra(const vector<vector<Edge>> g, int start)
{
    int n = (int) g.size();
    using P = pair<T, int>;
    vector<T> dist(n, numeric_limits<T>::max());
    priority_queue<P, vector<P>, greater<P>> pq;
    dist[start] = 0;
    pq.push({dist[start], start});
    while (!pq.empty())
    {
        T expected = pq.top().first;
        int i = pq.top().second;
        pq.pop();
        if (dist[i] != expected)
        {
            continue;
        }
        for (Edge e : g[i])
        {
            int j = e.to;
            T c = e.cost;
            if (dist[j] > dist[i] + c)
            {
                dist[j] = dist[i] + c;
                pq.push({dist[j], j});
            }
        }
    }
    return dist;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    vector<int> p(n), q(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> p[i] >> q[i];
    }
    vector<vector<Edge>> g(n);
    for (int i = 0; i < m; ++i)
    {
        int P, Q;
        cin >> P >> Q;
        --P;
        --Q;
        double dist = sqrt((p[P] - p[Q]) * (p[P] - p[Q]) + (q[P] - q[Q]) * (q[P] - q[Q]));
        g[P].push_back({Q, dist});
        g[Q].push_back({P, dist});
    }
    vector<double> dist = dijkstra<double>(g, x);
    cout << fixed << setprecision(17) << dist[y] << '\n';
    return 0;
}
0