結果

問題 No.3263 違法な散歩道
ユーザー hamikan
提出日時 2025-09-06 14:31:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 998 bytes
コンパイル時間 2,262 ms
コンパイル使用メモリ 209,232 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2025-09-06 14:31:29
合計ジャッジ時間 4,497 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N);
    vector<bool> used(N), exist(N);
    while(M--) {
        int U, V; cin >> U >> V;
        U--, V--;
        graph[U].push_back(V);
        graph[V].push_back(U);
    }
    int K;
    cin >> K;
    while(K--) {
        int A; cin >> A;
        exist[A-1] = true;
    }
    queue<tuple<int, int, ll>> now;
    vector<ll> ans(N, LLONG_MAX), cnt(N, 6);
    now.push({0, 0, 0});
    cnt[0] = 0;
    ans[0] = 0;
    while(!now.empty()) {
        auto [c, n, a] = now.front();
        now.pop();
        for(int to : graph[n]) {
            if(cnt[to] <= c + 1) continue;
            now.push({exist[to] ? c + 1 : 0, to, a + 1});
            cnt[to] = exist[to] ? c + 1 : 0;
            ans[to] = min(ans[to], a + 1);
        }
    }
    cout << (ans[N-1] == LLONG_MAX ? -1 : ans[N-1]) << endl;
}
0