結果

問題 No.1565 Union
ユーザー aaaa
提出日時 2022-10-27 17:20:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 603 bytes
コンパイル時間 1,986 ms
コンパイル使用メモリ 176,392 KB
実行使用メモリ 16,512 KB
最終ジャッジ日時 2024-11-23 17:23:46
合計ジャッジ時間 8,047 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  int a[m],b[m];
  vector<vector<int>> g(n);
  for(int i = 0;i < m;i++){
    cin>>a[i]>>b[i];
    a[i]--;
    b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }
  queue<int> q;
  q.push(0);
  vector<int> cost(n,1000000);
  cost[0] = 0;
  while(!q.empty()){
    int nn = q.front();
    q.pop();
    for(int x:g[nn]){
      if(cost[x] == 1000000){
        q.push(x);
        cost[x] = cost[nn]+1;
      }
    }
  }
  if(cost[n-1] == 1000000){
    cout<<-1<<endl;
  }
  else{
  cout<<cost[n-1]<<endl;
  }
}
0