結果

問題 No.1283 Extra Fee
ユーザー face4face4
提出日時 2020-12-05 17:58:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 1,352 bytes
コンパイル時間 650 ms
コンパイル使用メモリ 78,416 KB
実行使用メモリ 10,464 KB
最終ジャッジ日時 2024-04-27 23:44:26
合計ジャッジ時間 4,961 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 2 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 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 40 ms
5,376 KB
testcase_15 AC 65 ms
5,376 KB
testcase_16 AC 14 ms
5,376 KB
testcase_17 AC 139 ms
9,872 KB
testcase_18 AC 230 ms
8,056 KB
testcase_19 AC 250 ms
8,320 KB
testcase_20 AC 239 ms
7,808 KB
testcase_21 AC 245 ms
7,892 KB
testcase_22 AC 208 ms
7,552 KB
testcase_23 AC 186 ms
8,320 KB
testcase_24 AC 246 ms
8,320 KB
testcase_25 AC 257 ms
8,436 KB
testcase_26 AC 265 ms
8,448 KB
testcase_27 AC 257 ms
8,440 KB
testcase_28 AC 256 ms
8,448 KB
testcase_29 AC 147 ms
10,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, -1, 0, 1};

#define inRange(x,a,b) (a <= x && x < b)

int main(){
    int n, m;
    cin >> n >> m;
    int fee[n][n];
    memset(fee, 0, sizeof(fee));
    ll dp[2][n][n];
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0][0] = 0;
    while(m--){
        int x, y, z;
        cin >> x >> y >> z;
        fee[--x][--y] = z;
    }
    priority_queue<pair<ll,pii>> pq;
    pq.push({-0, {0, 0}});
    while(!pq.empty()){
        auto p = pq.top();  pq.pop();
        ll c = -p.first;
        int x = p.second.first, i = p.second.second/n, j = p.second.second%n;
        if(dp[x][i][j] != c)    continue;
        for(int k = 0; k < 4; k++){
            int ni = i+di[k], nj = j+dj[k];
            if(!inRange(ni,0,n)||!inRange(nj,0,n))  continue;
            ll nc = c+1+fee[ni][nj];
            if(dp[x][ni][nj] > nc){
                dp[x][ni][nj] = nc;
                pq.push({-nc, {x, ni*n+nj}});
            }
            if(x)   continue;
            nc -= fee[ni][nj];
            if(dp[1][ni][nj] > nc){
                dp[1][ni][nj] = nc;
                pq.push({-nc, {1, ni*n+nj}});
            }
        }
    }
    cout << dp[1][n-1][n-1] << endl;
    return 0;
}
0