結果

問題 No.1283 Extra Fee
ユーザー milanis48663220milanis48663220
提出日時 2020-11-06 21:58:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,008 bytes
コンパイル時間 1,093 ms
コンパイル使用メモリ 94,520 KB
実行使用メモリ 17,148 KB
最終ジャッジ日時 2023-09-29 18:42:22
合計ジャッジ時間 7,603 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 13 ms
6,400 KB
testcase_12 AC 13 ms
6,000 KB
testcase_13 AC 9 ms
5,812 KB
testcase_14 AC 35 ms
6,428 KB
testcase_15 AC 56 ms
6,640 KB
testcase_16 AC 13 ms
6,152 KB
testcase_17 AC 199 ms
15,500 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 180 ms
9,276 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 217 ms
16,004 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<int, 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