結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー granddaifuku
提出日時 2020-06-02 14:15:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 1,706 bytes
コンパイル時間 1,921 ms
コンパイル使用メモリ 181,276 KB
実行使用メモリ 22,400 KB
最終ジャッジ日時 2024-11-23 21:14:33
合計ジャッジ時間 9,163 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define FOR(i, a, b) for(int i = a; i < (int)b; ++i)
#define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i)

using ll = long long;
using ld = long double;

const ll INF = 1e18;
const double Inf = 1e9;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;

int n;
using P = pair<int, int>;

struct edge {
    int to;
    double cost;
};

vector<double> dist;
vector<vector<edge> > g;

void dijkstra(int s) {
    dist = vector<double>(n, Inf);
    dist[s] = 0;
    priority_queue<P, vector<P>, greater<P> > pq;
    pq.push(P(0, s));

    while (!pq.empty()) {
        P p = pq.top();
        pq.pop();
        int v = p.second;
        if (dist[v] < p.first) continue;

        rep (i, g[v].size()) {
            edge e = g[v][i];
            if (dist[e.to] > dist[v] + e.cost) {
                dist[e.to] = dist[v] + e.cost;
                pq.push(P(dist[e.to], e.to));
            }
        }
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    int m, X, Y;
    cin >> n >> m >> X >> Y;
    g.resize(n);
    vector<pair<double, double> > pole(n);
    rep (i, n) cin >> pole[i].first >> pole[i].second;
    rep (i, m) {
        int p, q;
        cin >> p >> q;
        p--, q--;
        double dx, dy;
        dx = pole[p].first - pole[q].first;
        dy = pole[p].second - pole[q].second;
        double c = sqrt(dx * dx + dy * dy);
        edge e;
        e.cost = c;
        e.to = q;
        g[p].push_back(e);
        e.to = p;
        g[q].push_back(e);
    }
    dijkstra(X - 1);
    cout << dist[Y - 1] << endl;
    
    return 0;
}

0