結果

問題 No.3263 違法な散歩道
ユーザー yantaro
提出日時 2025-09-11 00:23:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 2,049 bytes
コンパイル時間 2,270 ms
コンパイル使用メモリ 210,984 KB
実行使用メモリ 17,024 KB
最終ジャッジ日時 2025-09-11 00:23:21
合計ジャッジ時間 8,116 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 ld = long double;

//using mint = modint; //mint::set_mod(M);
//using mint = modint998244353;
//using mint = modint1000000007;

template<class T> using pq = priority_queue<T>; //大きい順
template<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>; //小さい順

#define vec_unique(v) v.erase(unique(v.begin(), v.end()), v.end()) //重複削除
#define vec_iota(v) iota(v.begin(), v.end(), 0) //0, 1, 2, 3, ..., n - 1にセット
#define concat(a, b) a.insert(a.end(), b.begin(), b.end())
#define debug(x) cerr << #x << " = " << x << endl

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
//int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
//int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

#define INF 2e18
#define INF2 2e9

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n);
    for(int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    int k;
    cin >> k;
    set<int> s;
    for(int i = 0; i < k; i++) {
        int a;
        cin >> a;
        a--;
        s.insert(a);
    }

    //幅優先探索 計算量O(n + m)
    vector<vector<int>> dist(n, vector<int>(5, INF2));
    queue<pair<int, int>> que;

    dist[0][0] = 0;
    que.push({0, 0});

    //グラフ
    while(!que.empty()) {
        auto[v, t] = que.front();
        que.pop();

        int d = dist[v][t] + 1;
        for(auto next_v : graph[v]) {
            int nt = 0;
            if(s.count(next_v)) nt = t + 1;
            if(nt == 5) continue;
            if(dist[next_v][nt] <= d) continue;

            dist[next_v][nt] = d;
            que.push({next_v, nt});
        }
    }

    int ans = INF2;
    for(int i = 0; i < 5; i++) ans = min(ans, dist[n - 1][i]);
    if(ans == INF2) cout << -1 << endl;
    else cout << ans << endl;
    //cout << fixed << setprecision(15) << << endl;
    return 0;
}
0