結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー のらら
提出日時 2025-10-28 19:36:51
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,820 bytes
コンパイル時間 2,827 ms
コンパイル使用メモリ 181,744 KB
実行使用メモリ 26,596 KB
最終ジャッジ日時 2025-11-01 09:27:42
合計ジャッジ時間 13,344 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

//最短経路をBFSで求めて最短経路上の頂点からの距離がK_i以上の頂点J_iが存在しないかどうかで判定(嘘解法)
#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;
  vector<ll> chk(N + 1, -1);
  for(int i = 0; i < L; i++){
    ll j, k;
    cin >> j >> k;
    chk[j] = k;
  }
  queue<ll> que;
  while(1){
    vector<ll> dist(N + 1, -1);
    if(chk[1] >= 0){
      cout << "No" << endl;
      return 0;
    }
    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;
      return 0;
    }
    ll now = N;
    bool isOK = true;
    while(1){
      vector<ll> visited(N + 1, -1);
      visited[now] = 0;
      que.push(now);
      while(!que.empty()){
        int pos = que.front(); que.pop();
        if(visited[pos] <= chk[pos]){
          chk[now] = 0;
          isOK = false;
        }
        for(auto to: G[pos]){
          if(visited[to] == -1){
            visited[to] = visited[pos] + 1;
            que.push(to);
          }
        }
      }
      if(now == 1) break;
      for(auto to: G[now]){
        if(dist[to] == dist[now] - 1){
          now = to;
          break;
        }
      }
    }
    if(!isOK) continue;
    cout << "Yes" << endl;
    cout << dist[N] << endl;
    break;
  }
  return 0;
}
0