結果

問題 No.1283 Extra Fee
ユーザー ShibuyapShibuyap
提出日時 2020-11-08 20:46:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 2,222 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 208,360 KB
実行使用メモリ 14,556 KB
最終ジャッジ日時 2024-04-27 23:38:44
合計ジャッジ時間 6,484 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 11 ms
6,944 KB
testcase_12 AC 12 ms
6,940 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 AC 44 ms
6,940 KB
testcase_15 AC 72 ms
6,944 KB
testcase_16 AC 15 ms
6,940 KB
testcase_17 AC 154 ms
13,880 KB
testcase_18 AC 238 ms
9,012 KB
testcase_19 AC 267 ms
9,352 KB
testcase_20 AC 254 ms
8,960 KB
testcase_21 AC 261 ms
9,076 KB
testcase_22 AC 229 ms
8,400 KB
testcase_23 AC 206 ms
9,300 KB
testcase_24 AC 274 ms
9,248 KB
testcase_25 AC 282 ms
9,508 KB
testcase_26 AC 277 ms
9,464 KB
testcase_27 AC 276 ms
9,600 KB
testcase_28 AC 278 ms
9,412 KB
testcase_29 AC 159 ms
14,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
#define yn {puts("YES");}else{puts("NO");}
#define MAX_N 200005

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int main() {
    int n, m;
    cin >> n >> m;
    ll a[n+2][n+2];
    rep(i,n+2){
        rep(j,n+2){
            if(i==0||i==n+1||j==0||j==n+1) a[i][j] = 1001001001001001001;
            else a[i][j] = 0;
        }
    }
    rep(i,m){
        int h, w, c;
        cin >> h >> w >> c;
        a[h][w] = c;
    }


    ll dp[n+2][n+2][2];
    rep(i,n+2){
        rep(j,n+2){
            rep(k,2){
                if(i==0||i==n+1||j==0||j==n+1) dp[i][j][k] = -1;
                else dp[i][j][k] = 1001001001001001001;
            }
        }
    }
    dp[1][1][0] = 0;

    pair<P,P> p;
    priority_queue<pair<P,P>,vector<pair<P,P>>,greater<pair<P,P>>> que;
    p.first.first = 0;
    p.first.second = 0;
    p.second.first = 1;
    p.second.second = 1;
    que.push(p);

    while(!que.empty()){
        p = que.top();
        que.pop();
        int x = p.second.first;
        int y = p.second.second;
        int k = p.first.second;
        if(p.first.first > dp[x][y][k]) continue;
        // cout << x << ' ' << y << ' ' << k << ' ' << dp[x][y][k] << endl;
        rep(i,4){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(dp[x][y][k]+a[nx][ny]+1<dp[nx][ny][k]){
                dp[nx][ny][k] = dp[x][y][k]+a[nx][ny]+1;
                p.first.first = dp[x][y][k]+a[nx][ny]+1;
                p.first.second = k;
                p.second.first = nx;
                p.second.second = ny;
                que.push(p);
            }
            if(k == 0 && dp[x][y][k]+1<dp[nx][ny][k+1]){
                dp[nx][ny][k+1] = dp[x][y][k]+1;
                p.first.first = dp[x][y][k]+1;
                p.second.first = nx;
                p.second.second = ny;
                p.first.second = k+1;
                que.push(p);
            }
        }
    }

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