結果

問題 No.1565 Union
ユーザー AuroraAurora
提出日時 2022-04-23 17:21:13
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 594 bytes
コンパイル時間 1,907 ms
コンパイル使用メモリ 174,572 KB
実行使用メモリ 15,088 KB
最終ジャッジ日時 2024-06-25 04:29:34
合計ジャッジ時間 6,834 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

int main(){
  int N,M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  for(int i=0;i<M;i++){
    int a,b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int INF = 1000000000;
  vector<int> dist(N,INF);
  dist[0] = 0;
  queue<int> que;
  que.push(0);
  while(!que.empty()){
    int v = que.front();
    que.pop();
    for(auto nv:G[v]){
      if(dist[nv] == INF){
        dist[nv] = dist[v]+1;
        que.push(nv);
      }
    }
  }
  int ans = dist[N-1];
  if(ans == INF) ans = -1;
  cout << ans << endl;
}
0