結果

問題 No.1283 Extra Fee
ユーザー trineutrontrineutron
提出日時 2020-11-21 18:20:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 441 ms / 2,000 ms
コード長 1,269 bytes
コンパイル時間 2,252 ms
コンパイル使用メモリ 214,828 KB
実行使用メモリ 17,484 KB
最終ジャッジ日時 2024-04-27 23:42:35
合計ジャッジ時間 8,663 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 17 ms
5,376 KB
testcase_12 AC 21 ms
5,376 KB
testcase_13 AC 14 ms
5,376 KB
testcase_14 AC 61 ms
5,376 KB
testcase_15 AC 100 ms
5,376 KB
testcase_16 AC 22 ms
5,376 KB
testcase_17 AC 312 ms
17,064 KB
testcase_18 AC 359 ms
8,960 KB
testcase_19 AC 388 ms
9,184 KB
testcase_20 AC 359 ms
8,704 KB
testcase_21 AC 368 ms
8,832 KB
testcase_22 AC 328 ms
8,320 KB
testcase_23 AC 366 ms
9,160 KB
testcase_24 AC 441 ms
9,216 KB
testcase_25 AC 400 ms
9,292 KB
testcase_26 AC 402 ms
9,276 KB
testcase_27 AC 403 ms
9,324 KB
testcase_28 AC 406 ms
9,284 KB
testcase_29 AC 343 ms
17,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using grid = vector<vector<int64_t>>;

vector<int> dx{1, 0, -1, 0}, dy{0, 1, 0, -1};

int main() {
    int n, m;
    cin >> n >> m;
    grid board(n, vector<int64_t>(n, 1));
    for (int i = 0; i < m; i++) {
        int h, w, c;
        cin >> h >> w >> c;
        board.at(h - 1).at(w - 1) += c;
    }
    vector<int64_t> cost(2 * n * n, 1L << 60);
    priority_queue<pair<int64_t, int>, vector<pair<int64_t, int>>, greater<pair<int64_t, int>>> q;
    q.emplace(0, 0);
    while (not q.empty()) {
        auto [d, id] = q.top();
        q.pop();
        if (d >= cost.at(id)) continue;
        cost.at(id) = d;
        int state = id / (n * n), row = id / n % n, column = id % n;
        for (int i = 0; i < 4; i++) {
            int newrow = row + dx[i], newcolumn = column + dy[i], newid = id + n * dx[i] + dy[i];
            if (newrow < 0 or n <= newrow or newcolumn < 0 or n <= newcolumn) continue;
            int64_t newd = d + board.at(newrow).at(newcolumn);
            if (newd < cost.at(newid)) q.emplace(newd, newid);
            if (state == 0) {
                if (d + 1 < cost.at(newid + n * n)) q.emplace(d + 1, newid + n * n);
            }
        }
    }
    cout << cost.at(2 * n * n - 1) << endl;
}
0