結果

問題 No.3263 違法な散歩道
ユーザー あいすあうと
提出日時 2025-09-06 14:34:10
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 3,032 bytes
コンパイル時間 6,764 ms
コンパイル使用メモリ 334,740 KB
実行使用メモリ 19,692 KB
最終ジャッジ日時 2025-09-06 14:34:26
合計ジャッジ時間 10,340 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using P = pair<ll,ll>;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vl = vc<ll>;
using vvl = vc<vc<ll>>;
using vvb = vc<vc<bool>>;
using vul = vc<ull>;
using vs = vc<string>;
using vb = vc<bool>;
#define rep(i,s,n) for(ll i=s;i<(n);i++)
#define Rep(i,s,n) for(ll i=n;i>=s;i--)
#define nall(x) x.begin(),x.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define nexp(v) next_permutation(v)
#define prep(v) prev_permutation(v)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define YN {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define M1 cout<<"-1"<<endl
const long long INF = (1LL<<62)-(1LL<<31)-1;
#define endl '\n'
using mint = modint998244353;
using mint7 = modint1000000007;
//vl dx = {1,-1,0,0};vl dy = {0,0,1,-1};
//vl dx={0,0,1,1,1,-1,-1,-1};vl dy={1,-1,0,1,-1,0,1,-1};
bool out_grid(ll i, ll j, ll h, ll w){return (!(0<=i && i<h && 0<=j && j<w));}
void chmin(ll &a,ll b){if(a>b)a=b;}
void chmax(ll &a,ll b){if(a<b)a=b;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll ceil(ll a,ll b){return (a+(b-1))/b;}
ll floor(ll a,ll b){return a/b;}
ll divisor_count_sum(ll N){	//約数の個数の総和
	ll sum=0,r=sqrt(N);
	rep(i,1,r+1){
		sum+=N/i;
	}
	return 2*sum-r*r;
}
ll phi(ll n){	//φ関数
	ll res=n;
	for(ll i=2;i*i<=n;i++){
		if(n%i==0){
			while(n%i==0)n/=i;
			res-=res/i;
		}
	}
	if(n>1)res-=res/n;
	return res;
}
vs rotate90(const vs& g){	//正方形を90度右回転
	ll len=g.size();
	vs x(len,string(len,'.'));
	rep(i,0,len){
		rep(j,0,len){
			x[j][len-1-i]=g[i][j];
		}
	}
	return x;
}
vl Eratos(ll n){	//篩
	vc<bool> isprime(n+1,true);
	isprime[0]=isprime[1]=false;
	for(ll i=2;i*i<=n;i++){
	  if(isprime[i]){
	    for(int j=i*i;j<=n;j+=i)isprime[j]=false;
	  }
	}
	vl primes;
	for(ll i=2;i<=n;i++){
		if(isprime[i])primes.pb(i);
	}
	return primes;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	ll n,m;
    cin >> n >> m;
    set<ll> ex;
    vvl g(n);
    rep(i,0,m){
        ll u,v;
        cin >> u >> v;
        u--;v--;
        g[u].pb(v);
        g[v].pb(u);
    }
    ll k;
    cin >> k;
    rep(i,0,k){
        ll a;
        cin >> a;
        a--;
        ex.insert(a);
    }
    vvb visited(n,vb(5,false));
    visited[0][0]=true;
    ll ans=INF;
    queue<tuple<ll,ll,ll>> q;
    q.push({0,0,0});
    while(!q.empty()){
        auto [now,iwhy,step]=q.front();
        q.pop();
        if(now==n-1){
            cout << step << endl;
            return 0;
        }
        for(auto next:g[now]){
            ll ni=ex.contains(next)?iwhy+1:0;
            if(ni>4)continue;
            if(visited[next][ni])continue;
            visited[next][ni]=true;
            q.push({next,ni,step+1});
        }
    }
    cout << -1 << endl;
}
0