結果

問題 No.1565 Union
ユーザー umezoumezo
提出日時 2021-06-26 13:20:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,110 bytes
コンパイル時間 2,178 ms
コンパイル使用メモリ 208,812 KB
実行使用メモリ 20,424 KB
最終ジャッジ日時 2023-09-07 16:27:16
合計ジャッジ時間 5,603 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 31 ms
9,596 KB
testcase_11 AC 76 ms
14,792 KB
testcase_12 AC 68 ms
13,788 KB
testcase_13 AC 23 ms
7,064 KB
testcase_14 AC 98 ms
16,092 KB
testcase_15 AC 174 ms
20,348 KB
testcase_16 AC 116 ms
19,772 KB
testcase_17 AC 179 ms
20,424 KB
testcase_18 AC 177 ms
20,372 KB
testcase_19 AC 172 ms
20,328 KB
testcase_20 AC 67 ms
18,840 KB
testcase_21 AC 65 ms
18,728 KB
testcase_22 AC 65 ms
18,580 KB
testcase_23 AC 64 ms
18,772 KB
testcase_24 AC 65 ms
18,484 KB
testcase_25 AC 73 ms
18,784 KB
testcase_26 AC 67 ms
18,836 KB
testcase_27 AC 70 ms
18,580 KB
testcase_28 AC 71 ms
18,728 KB
testcase_29 AC 68 ms
18,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n,m;
  cin>>n>>m;
  
  Graph G(n);
  rep(i,m){
    int a,b;
    cin>>a>>b;
    a--,b--;
    G[a].push_back(Edge(b,1));
    G[b].push_back(Edge(a,1));
  }
  
  vector<ll> dist(n,INF);
  int s=0;
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;
    
    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  
  if(dist[n-1]==INF) cout<<-1<<endl;
  else cout<<dist[n-1]<<endl;
  
  return 0;
}
0