結果

問題 No.1283 Extra Fee
ユーザー kappybarkappybar
提出日時 2020-11-06 22:22:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 1,832 bytes
コンパイル時間 1,706 ms
コンパイル使用メモリ 173,544 KB
実行使用メモリ 19,444 KB
最終ジャッジ日時 2024-04-27 23:19:52
合計ジャッジ時間 7,480 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 17 ms
6,944 KB
testcase_12 AC 19 ms
6,940 KB
testcase_13 AC 14 ms
6,944 KB
testcase_14 AC 54 ms
7,968 KB
testcase_15 AC 88 ms
8,424 KB
testcase_16 AC 19 ms
6,944 KB
testcase_17 AC 260 ms
17,644 KB
testcase_18 AC 319 ms
9,436 KB
testcase_19 AC 334 ms
9,536 KB
testcase_20 AC 316 ms
9,232 KB
testcase_21 AC 323 ms
9,472 KB
testcase_22 AC 281 ms
9,248 KB
testcase_23 AC 344 ms
9,472 KB
testcase_24 AC 411 ms
9,332 KB
testcase_25 AC 361 ms
9,448 KB
testcase_26 AC 356 ms
9,564 KB
testcase_27 AC 349 ms
9,608 KB
testcase_28 AC 341 ms
9,596 KB
testcase_29 AC 285 ms
19,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

ll dp[505][505][2];
ll check[505][505];

int main(){
    int n,m;
    cin >> n >> m;
    rep(i,m){
        int h,w;
        ll c;
        cin >> h >> w >> c;
        --h;--w;
        check[h][w] = c;
    }
    rep(i,n)rep(j,n)rep(k,2) dp[i][j][k] = -1;
    priority_queue<pll,vector<pll>,greater<pll>> pq;
    auto id = [&](int i,int j,int k){ return 2*(i+j*n)+k;};
    pq.push(pll(0,id(0,0,0)));

    while(!pq.empty()){
        pll now = pq.top();
        pq.pop();
        ll res = now.first;
        int i = (now.second / 2) % n;
        int j = (now.second / 2) / n;
        int k = (now.second % 2);
        if(dp[i][j][k] != -1) continue;

        dp[i][j][k] = res;
        rep(c,4){
            int now_i = i + dx[c];
            int now_j = j + dy[c];
            if(now_i < 0 || now_j < 0 || n <= now_i || n <= now_j) continue;
            if(check[now_i][now_j] == 0){
                if(dp[now_i][now_j][k] == -1) pq.push(pll(res+1,id(now_i,now_j,k)));
            }else{
                if(k == 0){
                    if(dp[now_i][now_j][k] == -1) pq.push(pll(res+1+check[now_i][now_j],id(now_i,now_j,k)));
                    if(dp[now_i][now_j][k+1] == -1) pq.push(pll(res+1,id(now_i,now_j,k+1)));
                }else{
                    if(dp[now_i][now_j][k] == -1) pq.push(pll(res+1+check[now_i][now_j],id(now_i,now_j,k)));
                }
            }
        }
    }

    cout << min(dp[n-1][n-1][0],dp[n-1][n-1][1]) << endl;
    return 0;
}
0