結果

問題 No.3263 違法な散歩道
ユーザー areik
提出日時 2025-09-06 14:05:31
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 1,552 bytes
コンパイル時間 4,465 ms
コンパイル使用メモリ 258,820 KB
実行使用メモリ 17,016 KB
最終ジャッジ日時 2025-09-06 14:05:39
合計ジャッジ時間 7,346 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using isize = size_t;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using f64 = long double;
using p2 = pair<i64, i64>;
using el = tuple<i64, i64, i64>;
using mint = atcoder::modint998244353;

void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
  _main();
}

i64 pow(i64 x, i64 n) {
  i64 res = 1;
  i64 t = x;
  while (n > 0) {
    if (n & 1) res *= t;
    t *= t;
    n >>= 1;
  }
  return res;
}

void _main() {
  i64 n, m;
  cin >> n >> m;
  vector<vector<i64>> g(n);
  for (i64 i = 0; i < m; i++) {
    i64 u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  i64 K;
  cin >> K;
  vector<bool> yiwiy(n, false);
  for (i64 i = 0; i < K; i++) {
    i64 a;
    cin >> a;
    a--;
    yiwiy[a] = true;
  }
  vector<vector<i64>> dp(n, vector<i64>(5, 1e18));
  dp[0][0] = 0;
  queue<p2> que;
  que.push({0, 0});
  i64 ans = 1e18;
  while (!que.empty()) {
    auto [i, j] = que.front();
    que.pop();
    for (i64 ni : g[i]) {
      i64 nj;
      if (yiwiy[ni]) nj = j + 1;
      else nj = 0;
      if (nj >= 5) continue;
      if (dp[ni][nj] > dp[i][j] + 1) {
        dp[ni][nj] = dp[i][j] + 1;
        que.push({ni, nj});
      }
    }
  }
  for (i64 i = 0; i < 5; i++) ans = min(ans, dp[n - 1][i]);
  if (ans == 1e18) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }
}
0