結果

問題 No.1565 Union
ユーザー rogi52rogi52
提出日時 2022-10-21 03:34:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 1,002 bytes
コンパイル時間 3,536 ms
コンパイル使用メモリ 206,692 KB
実行使用メモリ 15,992 KB
最終ジャッジ日時 2023-09-13 06:51:03
合計ジャッジ時間 7,250 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 28 ms
6,748 KB
testcase_11 AC 73 ms
12,220 KB
testcase_12 AC 60 ms
9,116 KB
testcase_13 AC 22 ms
5,888 KB
testcase_14 AC 92 ms
11,820 KB
testcase_15 AC 163 ms
15,912 KB
testcase_16 AC 106 ms
15,632 KB
testcase_17 AC 163 ms
15,992 KB
testcase_18 AC 153 ms
15,908 KB
testcase_19 AC 160 ms
15,964 KB
testcase_20 AC 61 ms
14,784 KB
testcase_21 AC 62 ms
14,844 KB
testcase_22 AC 63 ms
14,856 KB
testcase_23 AC 63 ms
14,904 KB
testcase_24 AC 62 ms
14,792 KB
testcase_25 AC 67 ms
14,788 KB
testcase_26 AC 63 ms
14,788 KB
testcase_27 AC 67 ms
14,788 KB
testcase_28 AC 67 ms
14,888 KB
testcase_29 AC 64 ms
14,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// g <- pair < v , cost > 
template < class T >
vector< T > dijkstra(vector<vector<pair<int, T>>> &graph, int s) {
    T INF = numeric_limits< T >::max();
    vector<T> dist(graph.size(), INF);
    priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;
    q.push({dist[s] = T(0), s});
    while(!q.empty()){
        auto [uc, ui] = q.top(); q.pop();
        if(uc != dist[ui]) continue;
        for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) 
            q.push({dist[vi] = uc + vc, vi});
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,M; cin >> N >> M;
    vector<vector<pair<int,int>>> G(N);
    rep(i,M) {
        int a,b; cin >> a >> b; a--; b--;
        G[a].push_back({b, 1});
        G[b].push_back({a, 1});
    }

    int ans = dijkstra(G, 0)[N - 1];
    cout << (ans < int(1e9) ? ans : -1) << endl;
}
0