結果

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

ソースコード

diff #

//全探索BFS+N→1(嘘解法)
#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;
  if(chk[N] >= 0){
    cout << "No" << endl;
    return 0;
  }
  vector<ll> dist(N + 1, -1);
  dist[N] = 0;
  que.push(N);
  while(!que.empty()){
    int pos = que.front(); que.pop();
    for(auto to: G[pos]){
      if(chk[to] == -1){
        queue<ll> que2;
        vector<ll> visited(N + 1, -1);
        bool isOK = true;
        que2.push(to);
        visited[to] = 0;
        while(!que2.empty()){
          int pos2 = que2.front(); que2.pop();
          if(!isOK) continue;
          if(visited[pos2] <= chk[pos2]){
            isOK = false;
            continue;
          }
          for(auto to2: G[pos2]){
            if(visited[to2] == -1){
              visited[to2] = visited[pos2] + 1;
              que2.push(to2);
            }
          }
        }
        if(!isOK){
          chk[to] = 0;
          continue;
        }
      }else{
        continue;
      }
      if(dist[to] == -1){
        dist[to] = dist[pos] + 1;
        que.push(to);
      }
    }
  }
  if(dist[1] == -1){
    cout << "No" << endl;
  }else{
    cout << "Yes" << endl;
    cout << dist[1] << endl;
  }
  return 0;
}
0