結果

問題 No.1283 Extra Fee
ユーザー milanis48663220milanis48663220
提出日時 2020-11-06 22:00:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 2,007 bytes
コンパイル時間 1,100 ms
コンパイル使用メモリ 95,480 KB
実行使用メモリ 16,084 KB
最終ジャッジ日時 2024-04-27 23:16:28
合計ジャッジ時間 5,307 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 14 ms
6,940 KB
testcase_12 AC 15 ms
6,944 KB
testcase_13 AC 10 ms
6,940 KB
testcase_14 AC 37 ms
6,944 KB
testcase_15 AC 60 ms
8,536 KB
testcase_16 AC 14 ms
6,944 KB
testcase_17 AC 213 ms
15,004 KB
testcase_18 AC 196 ms
9,328 KB
testcase_19 AC 210 ms
9,320 KB
testcase_20 AC 196 ms
9,332 KB
testcase_21 AC 200 ms
9,264 KB
testcase_22 AC 178 ms
9,108 KB
testcase_23 AC 194 ms
9,348 KB
testcase_24 AC 212 ms
9,172 KB
testcase_25 AC 215 ms
9,372 KB
testcase_26 AC 216 ms
9,516 KB
testcase_27 AC 217 ms
9,392 KB
testcase_28 AC 218 ms
9,364 KB
testcase_29 AC 232 ms
16,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

const ll INF = 1e+17;

typedef pair<ll, vector<int>> P;

struct edge{
    int to;
    ll cost;
};

int H, W;

int dh[4] = {0, 0, -1, 1};
int dw[4] = {-1, 1, 0, 0};

bool is_in_field(int i, int j){
    return i >= 0 && i < H && j >= 0 && j < W;
}

ll dist[500][500][2];
ll cost[500][500];

void input(){
    int M;
    cin >> H >> M;
    W = H;
    for(int i = 0; i < M; i++) {
        int h, w; cin >> h >> w; h--; w--;
        ll c; cin >> c;
        cost[h][w] = c;
    }    
}

void dijkstra(){
    priority_queue<P, vector<P>, greater<P>> que;
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            dist[i][j][0] = INF;
            dist[i][j][1] = INF;
        }
    }
    dist[0][0][0] = 0;
    vector<int> v = {0, 0, 0};
    que.push(P(0, v));
    while(!que.empty()){
        P p = que.top();que.pop();
        auto v = p.second;
        int h = v[0];
        int w = v[1];
        int s = v[2];
        ll d = p.first;
        if(dist[h][w][s] < d) continue;
        for(int i = 0; i < 4; i++){
            int h_to = h + dh[i];
            int w_to = w + dw[i];
            if(!is_in_field(h_to, w_to)) continue;
            if(d+cost[h_to][w_to]+1 < dist[h_to][w_to][s]){
                dist[h_to][w_to][s] = d+cost[h_to][w_to]+1;
                vector<int> v = {h_to, w_to, s};
                que.push(P(dist[h_to][w_to][s], v));
            }
            if(s == 0 && d+1 < dist[h_to][w_to][1]){
                dist[h_to][w_to][1] = d+1;
                vector<int> v = {h_to, w_to, 1};
                que.push(P(dist[h_to][w_to][1], v));
            }
        }
    }
}

void solve(){
    dijkstra();
    cout << min(dist[H-1][W-1][0], dist[H-1][W-1][1]) << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    input();
    solve();
}
0