結果

問題 No.3263 違法な散歩道
コンテスト
ユーザー srjywrdnprkt
提出日時 2025-10-20 16:07:55
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 1,306 bytes
コンパイル時間 3,522 ms
コンパイル使用メモリ 288,012 KB
実行使用メモリ 14,812 KB
最終ジャッジ日時 2025-10-20 16:08:02
合計ジャッジ時間 7,121 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/modint>

using namespace std;
//using namespace atcoder;
using ll = long long;
//using mint = modint998244353;

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int N, M;
    cin >> N >> M;
    vector<vector<int>> E(N);
    for (int i=0; i<M; i++){
        int u, v;
        cin >> u >> v;
        u--; v--;
        E[u].push_back(v);
        E[v].push_back(u);
    }
    int K;
    cin >> K;
    vector<bool> exist(N);
    for (int i=0; i<K; i++){
        int a;
        cin >> a;
        a--;
        exist[a] = 1;
    }
    vector dist(N, vector<int>(5, 1e9));
    dist[0][0] = 0;
    queue<pair<int, int>> que;
    que.push({0, 0});
    while(!que.empty()){
        auto [from, cnt] = que.front();
        que.pop();
        for (auto to : E[from]){
            if (!exist[to] && dist[to][0] == 1e9){
                dist[to][0] = dist[from][cnt]+1;
                que.push({to, 0});
            }
            else if (exist[to] && cnt <= 3 && dist[to][cnt+1] == 1e9){
                dist[to][cnt+1] = dist[from][cnt]+1;
                que.push({to, cnt+1});
            }
        }
    }
    int ans=1e9;
    for (int i=0; i<5; i++) ans = min(ans, dist[N-1][i]);
    cout << (ans == 1e9 ? -1 : ans) << endl;

    return 0;
}
0