結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー hatsuka_iwa
提出日時 2025-11-17 20:22:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 305 ms / 2,000 ms
コード長 1,077 bytes
コンパイル時間 2,287 ms
コンパイル使用メモリ 209,756 KB
実行使用メモリ 18,740 KB
最終ジャッジ日時 2025-11-17 20:22:57
合計ジャッジ時間 15,454 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M; cin >> N >> M;
  vector<int> dist(N, -1), Watch(N, -1);
  vector<vector<int>> Graph(N);
  priority_queue<pair<int, int>> PQ;

  for (int i = 0; i < M; i++) {
    int u, v; cin >> u >> v;
    Graph.at(u - 1).push_back(v - 1);
    Graph.at(v - 1).push_back(u - 1);
  }
  int L; cin >> L;
  for (int i = 0; i < L; i++) {
    int J, K; cin >> J >> K;
    PQ.push({K, J - 1});
  }

  while (!PQ.empty()) {
    int J, K; tie(K, J) = PQ.top();
    PQ.pop();
    if (Watch.at(J) >= K) continue;
    Watch.at(J) = K;
    for (int nv : Graph.at(J))
      if (K - 1 > Watch.at(nv)) PQ.push({K - 1, nv});
  }

  queue<int> Q;
  dist.at(0) = 0;
  if (Watch.at(0) == -1) Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int nv : Graph.at(v)) {
      if (dist.at(nv) != - 1 || Watch.at(nv) != -1) continue;
      dist.at(nv) = dist.at(v) + 1;
      Q.push(nv);
    }
  }

  cout << (dist.at(N - 1) != -1 ? "Yes" : "No") << endl;
  if (dist.at(N - 1) != -1) cout << dist.at(N - 1) << endl;
}
0