結果

問題 No.3263 違法な散歩道
ユーザー こめだわら
提出日時 2025-08-15 15:34:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 1,943 bytes
コンパイル時間 3,650 ms
コンパイル使用メモリ 289,968 KB
実行使用メモリ 17,024 KB
最終ジャッジ日時 2025-08-15 15:34:23
合計ジャッジ時間 7,786 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define rep(i,n) for(ll i=0;i<n;++i)
#define all(a) (a).begin(),(a).end()
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }
template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }


void solve() {
    ll N,M;
    cin>>N>>M;
    vector<vector<ll>> edge(N);
    rep(_,M){
        ll u,v;
        cin>>u>>v;
        u--;
        v--;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    vector<bool> iwai(N,false);
    ll K;
    cin>>K;
    rep(i,K){
        ll a;
        cin>>a;
        a--;
        iwai[a]=true;
    }
    vector<vector<ll>> dist(N,vector<ll> (5,1e9));
    dist[0][0]=0;
    queue<pair<ll,ll>> qu;
    qu.push({0,0});
    while (qu.size()>0){
        auto [cp,ck]=qu.front();qu.pop();
        for (auto np:edge[cp]){
            if (iwai[np]){
                if (ck==4)continue;
                if (chmin(dist[np][ck+1],dist[cp][ck]+1)){
                    qu.push({np,ck+1});
                }
            }
            else{
                if (chmin(dist[np][0],dist[cp][ck]+1)){
                    qu.push({np,0});
                }
            }
        }
    }
    ll ans=*min_element(all(dist[N-1]));
    if (ans>=1e8){
        cout<<-1<<endl;
    }
    else{
        cout<<ans<<endl;
    }
    return;
}


int main() {
    ll T=1;
    while (T--){
        solve();
    }
    return 0;
}
0