結果

問題 No.1565 Union
ユーザー take000take000
提出日時 2021-07-02 23:13:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 2,229 ms
コンパイル使用メモリ 211,084 KB
実行使用メモリ 16,168 KB
最終ジャッジ日時 2023-09-11 23:39:49
合計ジャッジ時間 7,305 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,500 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 63 ms
6,732 KB
testcase_11 AC 124 ms
12,264 KB
testcase_12 AC 132 ms
9,148 KB
testcase_13 AC 40 ms
5,768 KB
testcase_14 AC 166 ms
11,916 KB
testcase_15 AC 244 ms
15,832 KB
testcase_16 AC 192 ms
15,308 KB
testcase_17 AC 243 ms
16,168 KB
testcase_18 AC 241 ms
15,852 KB
testcase_19 AC 239 ms
15,892 KB
testcase_20 AC 138 ms
14,780 KB
testcase_21 AC 139 ms
14,832 KB
testcase_22 AC 138 ms
14,780 KB
testcase_23 AC 139 ms
14,872 KB
testcase_24 AC 138 ms
14,952 KB
testcase_25 AC 145 ms
14,704 KB
testcase_26 AC 138 ms
14,876 KB
testcase_27 AC 142 ms
14,932 KB
testcase_28 AC 141 ms
14,912 KB
testcase_29 AC 139 ms
14,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;

struct edge {
    int to;
    int cost;
};
using P = pair<int, int>;
vector<vector<edge>> G;
vector<int> d;
void dijkstra(int s) {
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d.begin(), d.end(), 1e9);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (d[v] < p.first)
            continue;
        rep(i, G[v].size()) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    G.resize(N);
    d.resize(N);
    rep(i, M) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].push_back({b, 1});
        G[b].push_back({a, 1});
    }

    dijkstra(0);

    if (d[N - 1] == 1e9) d[N - 1] = -1;
    cout << d[N - 1] << endl;

    return 0;
}
0