結果

問題 No.1283 Extra Fee
ユーザー lorent_kyoprolorent_kyopro
提出日時 2020-11-08 14:43:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 2,846 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 223,376 KB
実行使用メモリ 21,216 KB
最終ジャッジ日時 2024-04-27 23:37:25
合計ジャッジ時間 5,625 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 9 ms
6,940 KB
testcase_12 AC 10 ms
6,940 KB
testcase_13 AC 7 ms
6,944 KB
testcase_14 AC 29 ms
6,940 KB
testcase_15 AC 47 ms
7,424 KB
testcase_16 AC 10 ms
6,944 KB
testcase_17 AC 155 ms
19,712 KB
testcase_18 AC 165 ms
17,024 KB
testcase_19 AC 166 ms
17,536 KB
testcase_20 AC 156 ms
16,768 KB
testcase_21 AC 157 ms
16,768 KB
testcase_22 AC 141 ms
15,488 KB
testcase_23 AC 146 ms
17,920 KB
testcase_24 AC 162 ms
17,920 KB
testcase_25 AC 171 ms
18,048 KB
testcase_26 AC 170 ms
18,048 KB
testcase_27 AC 174 ms
18,048 KB
testcase_28 AC 171 ms
18,048 KB
testcase_29 AC 169 ms
21,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
using namespace std;
using ll = long long;
template<typename T>
inline bool chmax(T& a, const T& b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
template<typename T>
inline bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */
namespace detail {
    template<typename T, int N>
    auto make_vec(vector<int>& sizes, T const& x) {
        if constexpr (N == 1) {
            return vector(sizes[0], x);
        } else {
            int size = sizes[N-1];
            sizes.pop_back();
            return vector(size, make_vec<T, N-1>(sizes, x));
        }
    }
}
template<typename T, int N>
auto make_vec(int const(&sizes)[N], T const& x = T()) {
    vector<int> s(N);
    for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1];
    return detail::make_vec<T, N>(s, x);
}
__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

int main() {
    int n, m;
    cin >> n >> m;
    auto grid = make_vec<int>({n, n});
    rep(i, m) {
        int h, w, c;
        cin >> h >> w >> c;
        h--; w--;
        grid[h][w] = c;
    }

    auto dp = make_vec<ll>({n, n, 2}, LLONG_MAX);
    dp[0][0][0] = 0;
    using T = tuple<ll, int, int, int>;
    priority_queue<T, vector<T>, greater<T>> pq;
    pq.emplace(dp[0][0][0], 0, 0, 0);

    array<int, 4> di = {0, 1, 0, -1}, dj = {1, 0, -1, 0};
    while (!pq.empty()) {
        auto [cost, i, j, state] = pq.top();
        pq.pop();
        if (dp[i][j][state] < cost) continue;
        rep(k, 4) {
            int ni = i + di[k], nj = j + dj[k];
            if (ni < 0 || nj < 0 || ni >= n || nj >= n) continue;
            if (grid[ni][nj] == 0) {
                ll ncost = cost + 1;
                int nstate = state;
                if (chmin(dp[ni][nj][nstate], ncost)) {
                    pq.emplace(ncost, ni, nj, nstate);
                }
            } else {
                if (state == 0) {
                    ll ncost = cost + 1;
                    int nstate = 1;
                    if (chmin(dp[ni][nj][nstate], ncost)) {
                        pq.emplace(ncost, ni, nj, nstate);
                    }
                }
                ll ncost = cost + 1 + grid[ni][nj];
                int nstate = state;
                if (chmin(dp[ni][nj][nstate], ncost)) {
                    pq.emplace(ncost, ni, nj, nstate);
                }
            }
        }
    }
    cout << min(dp[n-1][n-1][0], dp[n-1][n-1][1]) << '\n';
}
0