結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー Rumain831
提出日時 2025-11-01 17:01:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 1,118 bytes
コンパイル時間 1,218 ms
コンパイル使用メモリ 91,500 KB
実行使用メモリ 18,088 KB
最終ジャッジ日時 2025-11-01 17:01:37
合計ジャッジ時間 10,853 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
#include<tuple>
#include<algorithm>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main(void){
  int n, m; cin >> n >> m;
  vector<vector<int>> to(n);
  for(int i=0; i<m; i++){
    int u, v; cin >> u >> v; u--, v--;
    to[u].push_back(v);
    to[v].push_back(u);
  }
  priority_queue<P> pri;
  vector<int> ng(n);
  int l; cin >> l;
  for(int i=0; i<l; i++){
    int j, k; cin >> j >> k; j--;
    pri.emplace(k+1, j);
    ng[j]=max(ng[j], k+1);
  }
  while(pri.size()){
    auto [d, id]=pri.top(); pri.pop();
    if(ng[id]>d) continue;
    if(d==1) continue;
    for(auto p:to[id]){
      if(ng[p]<d-1){
        ng[p]=d-1;
        pri.emplace(d-1, p);
      }
    }
  }
  queue<P> bfs;
  vector<int> dist(n, 1e9);
  bfs.emplace(0, 0);
  while(bfs.size()){
    auto [id, d]=bfs.front(); bfs.pop();
    if(dist[id]!=1e9) continue;
    if(ng[id]) continue;
    dist[id]=d;
    for(auto p:to[id]){
      bfs.emplace(p, d+1);
    }
  }
  if(dist[n-1]==1e9) cout << "No" << endl;
  else cout << "Yes" << endl << dist[n-1] << endl;
  return 0;
}
0