結果

問題 No.3263 違法な散歩道
ユーザー The Forsaking
提出日時 2025-09-07 12:50:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,461 bytes
コンパイル時間 2,007 ms
コンパイル使用メモリ 200,732 KB
実行使用メモリ 18,612 KB
最終ジャッジ日時 2025-09-07 12:50:38
合計ジャッジ時間 4,665 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:22:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   22 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
main.cpp:28:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
const int N = 2000086, MOD = 998244353, INF = 0x3f3f3f3f;
ll res;
int n, m, cnt, w[N];


int e[N], ne[N], h[N], idx;
bool st[N], g[100010][5];
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

int d[100010][5];
int main() {
    memset(d, 0x3f, sizeof d);
    cin >> n >> m;
    memset(h, -1, sizeof(int) * (n + 10));
    for (int i = 1, a, b; i < m + 1; i++) {
        scanf("%d%d", &a, &b);
        add(a, b), add(b, a);
    }
    cin >> m;
    while (m--) {
        int x;
        scanf("%d", &x);
        st[x] = 1;
    }
    d[1][0] = 0;
    priority_queue<array<int, 3>, vector<array<int, 3> >, greater<array<int, 3> > > q;
    q.push({0, 1, 0});
    while (q.size()) {
        auto u = q.top(); q.pop();
        if (g[u[1]][u[2]]) continue;
        g[u[1]][u[2]] = 1;
        for (int i = h[u[1]]; ~i; i = ne[i]) {
            int j = e[i];
            if (st[j]) {
                if (u[2] == 4) continue;
                if (d[j][u[2] + 1] > u[0] + 1) {
                    d[j][u[2] + 1] = u[0] + 1;
                    q.push({u[0] + 1, j, u[2] + 1});
                }
            } else {
                if (d[j][0] > u[0] + 1) {
                    d[j][0] = u[0] + 1;
                    q.push({u[0] + 1, j, 0});
                }
            }
        }
    }
    printf("%d\n", d[n][0] == INF ? -1 : d[n][0]);
    return 0;
}
0