結果

問題 No.2673 A present from B
ユーザー Today03Today03
提出日時 2024-03-15 23:25:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 2,117 bytes
コンパイル時間 4,374 ms
コンパイル使用メモリ 229,428 KB
実行使用メモリ 21,968 KB
最終ジャッジ日時 2024-03-15 23:25:34
合計ジャッジ時間 5,154 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 87 ms
21,968 KB
testcase_07 AC 89 ms
21,968 KB
testcase_08 AC 89 ms
21,968 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 27 ms
8,576 KB
testcase_13 AC 26 ms
8,576 KB
testcase_14 AC 43 ms
12,032 KB
testcase_15 AC 16 ms
6,676 KB
testcase_16 AC 15 ms
6,676 KB
testcase_17 AC 10 ms
6,676 KB
testcase_18 AC 17 ms
6,676 KB
testcase_19 AC 3 ms
6,676 KB
testcase_20 AC 35 ms
10,496 KB
testcase_21 AC 71 ms
17,696 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#ifdef LOCAL
#include "./debug.cpp"
#else
#define debug(...)
#define print_line
#endif
using namespace std;
using ll = long long;

template <typename T>
vector<T> dijkstra(const vector<vector<pair<int, T>>> &G, int start = 0) {
    int n = G.size();
    vector<T> dst(n, numeric_limits<T>::max());
    priority_queue<pair<T, int>> pq;
    dst[start] = 0;
    pq.push(make_pair(0, start));
    while (!pq.empty()) {
        auto [dst_sum, now] = pq.top();
        pq.pop();
        dst_sum = -dst_sum;
        if (dst[now] < dst_sum) continue;
        for (auto [nxt, cost] : G[now]) {
            if (dst[nxt] > dst[now] + cost) {
                dst[nxt] = dst[now] + cost;
                pq.push(make_pair(-dst[nxt], nxt));
            }
        }
    }
    return dst;
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> A(M);
    for (int i = 0; i < M; i++) {
        cin >> A[i];
        A[i]--;
    }

    vector<vector<pair<int, int>>> G(N * (M + 1));

    vector<int> idx(N);
    iota(idx.begin(), idx.end(), 0);

    for (int i = 0; i <= M; i++) {
        for (int j = 0; j < N - 1; j++) {
            G[i * N + j].push_back({i * N + j + 1, 1});
            G[i * N + j + 1].push_back({i * N + j, 1});
        }
        if (i == M) {
            break;
        }
        G[(i + 1) * N + A[i] + 1].push_back({i * N + A[i], 0});
        G[(i + 1) * N + A[i]].push_back({i * N + A[i] + 1, 0});
        for (int j = 0; j < N; j++) {
            if (j == A[i] || j == A[i] + 1) {
                continue;
            }
            G[(i + 1) * N + j].push_back({i * N + j, 0});
        }
    }

    auto dst = dijkstra(G, M * N);
    for (int i = 1; i < N; i++) {
        if (i > 1) {
            cout << ' ';
        }
        cout << dst[i];
    }
    cout << endl;

#ifdef LOCAL
    for (int i = 0; i <= M; i++) {
        cout << i << endl;
        for (int j = 0; j < N; j++) {
            cout << j << ':' << dst[i * N + j] << ' ';
        }
        cout << endl;
    }
#endif
}
0