結果

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

ソースコード

diff #

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

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, int>> now;
    vector<int> ans(N, INT_MAX), cnt(N, 5);
    now.push({0, 0, 0});
    cnt[0] = 0;
    ans[0] = 0;
    while(!now.empty()) {
        auto [c, n, a] = now.front();
        now.pop();
        bool safe = false;
        for(int to : graph[n]) safe |= !exist[to];
        if(safe && cnt[n] > 1) {
            now.push({1, n, a + 2});
            cnt[n] = 1;
        }
        for(int to : graph[n]) {
            if(cnt[to] <= c + 1) continue;
            c = (exist[to] ? c + 1 : 0);
            now.push({c, to, a + 1});
            cnt[to] = c;
            ans[to] = min(ans[to], a + 1);
        }
    }
    cout << (ans[N-1] == INT_MAX ? -1 : ans[N-1]) << endl;
}
0