結果

問題 No.1283 Extra Fee
ユーザー carrot46carrot46
提出日時 2020-11-12 19:38:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 1,849 bytes
コンパイル時間 1,599 ms
コンパイル使用メモリ 182,136 KB
実行使用メモリ 13,184 KB
最終ジャッジ日時 2024-04-27 23:41:14
合計ジャッジ時間 6,281 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 10 ms
6,940 KB
testcase_12 AC 13 ms
6,940 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 AC 43 ms
6,944 KB
testcase_15 AC 67 ms
6,940 KB
testcase_16 AC 15 ms
6,940 KB
testcase_17 AC 146 ms
12,544 KB
testcase_18 AC 236 ms
10,496 KB
testcase_19 AC 258 ms
11,008 KB
testcase_20 AC 242 ms
10,496 KB
testcase_21 AC 243 ms
10,624 KB
testcase_22 AC 225 ms
9,728 KB
testcase_23 AC 193 ms
11,136 KB
testcase_24 AC 258 ms
11,264 KB
testcase_25 AC 260 ms
11,264 KB
testcase_26 AC 270 ms
11,136 KB
testcase_27 AC 259 ms
11,264 KB
testcase_28 AC 278 ms
11,136 KB
testcase_29 AC 156 ms
13,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}

int main(){
    cin>>N>>M;
    mat grid(N, vec(N, 0));
    rep(i, M){
        int h, w; cin>>h>>w;
        --h; --w;
        cin>>grid[h][w];
    }
    vector<mat> dist(2, mat(N, vec(N, INF)));
    dist[0][0][0] = 0;
    priority_queue<P, vector<P>, greater<> > pque;
    pque.emplace(0, (0 * N + 0) * N + 0);
    vec dh = {-1, 0, 1, 0}, dw = {0, -1, 0, 1};
    while(!pque.empty()){
        P p = pque.top(); pque.pop();
        ll d = p.first, flag = p.se >= N * N, h = (p.se % (N * N)) / N, w = p.se % N;
        if(dist[flag][h][w] < d) continue;
        //cout<<d<<' '<<flag<<' '<<h<<' '<<w<<endl;
        rep(i, 4){
            int nh = h + dh[i], nw = w + dw[i];
            if(nh >= 0 && nh < N && nw >= 0 && nw < N){
                if(chmin(dist[flag][nh][nw], d + 1 + grid[nh][nw])){
                    pque.emplace(dist[flag][nh][nw], (flag * N + nh) * N + nw);
                }
                if(flag == 0 && grid[nh][nw] && chmin(dist[1][nh][nw], d + 1)){
                    pque.emplace(dist[1][nh][nw], (N + nh) * N + nw);
                }
            }
        }
    }
    cout<<min(dist[0][N-1][N-1], dist[1][N-1][N-1])<<endl;
}
0