結果

問題 No.3263 違法な散歩道
ユーザー shingo0909
提出日時 2025-09-06 13:25:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 942 bytes
コンパイル時間 3,437 ms
コンパイル使用メモリ 288,696 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2025-09-06 13:25:52
合計ジャッジ時間 7,693 ms
ジャッジサーバーID
(参考情報)
judge / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <cstdlib>
#include <math.h>
using namespace std;

using ll = long long;


int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<int>>g(n);
    vector<bool>a(n);
    for(int i=0;i<m;i++){
        int u,v;
        cin >> u >> v;
        u--;v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int k;
    cin >> k;
    for(int i=0;i<k;i++){
        int x;
        cin >> x;
        x--;
        a[x]=true;
    }
    vector dist(n,vector<int>(5,-1));
    dist[0][0]=0;
    queue<pair<int,int>>q;
    q.push({0,0});
    while(!q.empty()){
        auto [x,d]=q.front();q.pop();
        for(int y:g[x]){
            int nd=0;
            if(a[y]){
                nd=d+1; 
            }
            if(nd==5)continue;
            if(dist[y][nd]!=-1)continue;
            dist[y][nd]=dist[x][d]+1;
            q.push({y,nd});
        }
    }
    cout << dist[n-1][0] << endl;
    return 0;
}
0