結果

問題 No.1283 Extra Fee
ユーザー face4face4
提出日時 2020-12-05 17:58:57
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 1,352 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 77,552 KB
実行使用メモリ 10,332 KB
最終ジャッジ日時 2024-11-16 07:15:20
合計ジャッジ時間 5,096 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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