結果

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

ソースコード

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();
        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] == INT_MAX ? -1 : ans[N-1]) << endl;
}
0