結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー river0525
提出日時 2026-06-19 15:19:48
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 1,603 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,513 ms
コンパイル使用メモリ 346,432 KB
実行使用メモリ 18,108 KB
最終ジャッジ日時 2026-06-19 15:20:00
合計ジャッジ時間 9,658 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <iomanip>
#include <numbers>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using l3 = __int128;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using tint = tuple<int, int, int>;
using tll = tuple<ll, ll, ll>;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <typename T>
bool chmax(T &a, const T &b){if(a<b){a=b;return true;}return false;}
template <typename T>
bool chmin(T &a, const T &b){if(a>b){a=b;return true;}return false;}
const int inf = 1e9;
const ll linf = 1e18;
const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};

int main(){
  int n,m;cin>>n>>m;
  vector<vector<int>> graph(n);
  rep(i,m){
    int u,v;cin>>u>>v;
    u--,v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  int l;cin>>l;
  vector<int> dp(n,1);
  rep(i,l){
    int j,k;cin>>j>>k;
    j--;
    chmin(dp[j],-k);
  }
  min_priority_queue<pint> pq;
  rep(i,n)if(dp[i]<0)pq.emplace(dp[i],i);
  while(pq.size()){
    auto [d,v]=pq.top();pq.pop();
    if(dp[v]<d)continue;
    int nd=d+1;
    for(auto& nv:graph[v]){
      if(nd==1||dp[nv]<=nd)continue;
      pq.emplace(dp[nv]=nd,nv);
    }
  }
  queue<int> q;
  vector<int> dist(n,inf);
  if(dp[0]==1){
    q.push(0);
    dist[0]=0;
  }
  while(q.size()){
    int v=q.front();q.pop();
    for(auto& nv:graph[v]){
      if(dp[nv]!=1||dist[nv]<=dist[v]+1)continue;
      dist[nv]=dist[v]+1;
      q.emplace(nv);
    }
  }
  if(dist[n-1]==inf)cout<<"No\n";
  else cout<<"Yes\n"<<dist[n-1]<<endl;
}
0