結果
問題 | No.1565 Union |
ユーザー |
|
提出日時 | 2022-10-21 03:34:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 187 ms / 2,000 ms |
コード長 | 1,002 bytes |
コンパイル時間 | 2,411 ms |
コンパイル使用メモリ | 202,304 KB |
最終ジャッジ日時 | 2025-02-08 09:33:42 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#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; }