結果

問題 No.1283 Extra Fee
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-04-04 12:36:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 1,520 bytes
コンパイル時間 2,745 ms
コンパイル使用メモリ 227,864 KB
実行使用メモリ 40,696 KB
最終ジャッジ日時 2024-04-04 12:37:01
合計ジャッジ時間 9,445 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 15 ms
6,676 KB
testcase_12 AC 14 ms
6,676 KB
testcase_13 AC 10 ms
6,676 KB
testcase_14 AC 45 ms
9,344 KB
testcase_15 AC 65 ms
12,800 KB
testcase_16 AC 13 ms
6,676 KB
testcase_17 AC 244 ms
37,580 KB
testcase_18 AC 236 ms
34,304 KB
testcase_19 AC 251 ms
35,712 KB
testcase_20 AC 253 ms
33,664 KB
testcase_21 AC 235 ms
34,176 KB
testcase_22 AC 225 ms
30,848 KB
testcase_23 AC 243 ms
36,736 KB
testcase_24 AC 260 ms
36,736 KB
testcase_25 AC 263 ms
36,736 KB
testcase_26 AC 281 ms
36,736 KB
testcase_27 AC 260 ms
36,736 KB
testcase_28 AC 262 ms
36,736 KB
testcase_29 AC 282 ms
40,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    ll N, M, h, w, x, nh, nw, d, alt;
    bool f;
    cin >> N >> M;
    vector c(N, vector<ll>(N));
    for (int i=0; i<M; i++){
        cin >> h >> w >> x;
        h--; w--;
        c[h][w] = x;
    }

    pq<tuple<ll, int, int, bool>> que;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    vector dist(N, vector(N, vector<ll>(2, 1e18)));
    vector vst(N, vector(N, vector<bool>(2, 0)));

    dist[0][0][0] = 0;
    que.push({0, 0, 0, 0});

    while(!que.empty()){
        tie(d, h, w, f) = que.top();
        que.pop();
        if (vst[h][w][f]) continue;
        vst[h][w][f] = 1;
        for (int dir=0; dir<4; dir++){
            nh = h + dx[dir];
            nw = w + dy[dir];
            if (nh < 0 || nh >= N || nw < 0 || nw >= N) continue;
            alt = dist[h][w][f]+1+c[nh][nw];
            if (alt < dist[nh][nw][f]){
                dist[nh][nw][f] = alt;
                que.push({dist[nh][nw][f], nh, nw, f});
            }
            if (f == 0){
                alt = dist[h][w][f]+1;
                if (alt < dist[nh][nw][1]){
                    dist[nh][nw][1] = alt;
                    que.push({dist[nh][nw][1], nh, nw, 1});
                }
            }
        }
    }

    cout << min(dist[N-1][N-1][0], dist[N-1][N-1][1]) << endl;

    return 0;
}
0