結果

問題 No.1283 Extra Fee
ユーザー trineutron
提出日時 2020-11-21 18:20:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 454 ms / 2,000 ms
コード長 1,269 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 205,700 KB
最終ジャッジ日時 2025-01-16 04:06:41
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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