結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー のらら
提出日時 2025-10-26 21:24:31
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,167 ms / 2,000 ms
+ 686µs
コード長 1,274 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,094 ms
コンパイル使用メモリ 270,416 KB
実行使用メモリ 23,232 KB
最終ジャッジ日時 2026-07-16 03:52:05
合計ジャッジ時間 12,641 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

//多始点BFS(嘘解法)
#include <iostream>
#include <algorithm>
#include <atcoder/all>
#include <queue>
using namespace std;
using namespace atcoder;
using ll = long long;
//#define endl "\n";
ll N, M, L;
vector<vector<int>> G;

int main(){
  cin >> N >> M;
  G.resize(N + 1);
  for(int i = 0; i < M; i++){
    ll a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cin >> L;
  queue<pair<ll, ll>> quep;
  vector<ll> chk(N + 1, -1);
  for(int i = 0; i < L; i++){
    ll j, k;
    cin >> j >> k;
    chk[j] = k;
    quep.push({j, chk[j]});
  }
  while(!quep.empty()){
    auto [pos, d] = quep.front(); quep.pop();
    for(auto to: G[pos]){
      if(chk[to] < d - 1){
        chk[to] = d - 1;
        quep.push({to, chk[to]});
      }
    }
  }
  queue<ll> que;
  vector<ll> dist(N + 1, -1);
  if(chk[1] != -1){
    cout << "No" << endl;
  }else{
    dist[1] = 0;
    que.push(1);
    while(!que.empty()){
      int pos = que.front(); que.pop();
      for(auto to: G[pos]){
        if(dist[to] == -1 && chk[to] == -1){
          dist[to] = dist[pos] + 1;
          que.push(to);
        }
      }
    }
    if(dist[N] == -1){
      cout << "No" << endl;
    }else{
      cout << "Yes" << endl;
      cout << dist[N] << endl;
    }
  }
  return 0;
}
0