結果

問題 No.3263 違法な散歩道
コンテスト
ユーザー 👑 ONPU3
提出日時 2026-10-05 23:50:45
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 40 ms / 2,000 ms
+ 829µs
コード長 1,033 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,404 ms
コンパイル使用メモリ 349,608 KB
実行使用メモリ 17,080 KB
最終ジャッジ日時 2026-10-05 23:50:54
合計ジャッジ時間 6,531 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    ll n,m;cin>>n>>m;
    vector<vector<ll>>g(n);
    for(int i=0;i<m;i++)
    {
        ll u,v;cin>>u>>v;
        u--;v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    ll k;cin>>k;
    vector<bool>isa(n);
    for(int i=0;i<k;i++)
    {
        ll a;cin>>a;
        a--;
        isa[a]=true;
    }
    queue<pair<ll,ll>>que;
    vector dist(n,vector<ll>(5,-1));
    que.push({0,0});
    dist[0][0]=0;
    while(!que.empty())
    {
        auto[now,cnt]=que.front();
        que.pop();
        for(ll nex:g[now])
        {
            ll cntnex=0;
            if(isa[nex])cntnex=cnt+1;
            if(cntnex==5)continue;
            if(dist[nex][cntnex]!=-1)continue;
            dist[nex][cntnex]=dist[now][cnt]+1;
            que.push({nex,cntnex});
        }
    }
    ll ans=1LL<<60;
    for(auto o:dist[n-1])if(o!=-1)ans=min(ans,o);
    if(ans==1LL<<60)cout<<-1<<endl;
    else cout<<ans<<endl;
}
0