結果

問題 No.1283 Extra Fee
ユーザー nawawannawawan
提出日時 2020-11-07 12:17:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 309 ms / 2,000 ms
コード長 1,579 bytes
コンパイル時間 786 ms
コンパイル使用メモリ 90,160 KB
実行使用メモリ 16,456 KB
最終ジャッジ日時 2024-04-27 23:34:20
合計ジャッジ時間 5,682 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,948 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 17 ms
6,944 KB
testcase_12 AC 14 ms
6,940 KB
testcase_13 AC 10 ms
6,940 KB
testcase_14 AC 44 ms
6,944 KB
testcase_15 AC 68 ms
6,944 KB
testcase_16 AC 15 ms
6,944 KB
testcase_17 AC 278 ms
16,456 KB
testcase_18 AC 233 ms
13,824 KB
testcase_19 AC 263 ms
14,592 KB
testcase_20 AC 239 ms
13,824 KB
testcase_21 AC 251 ms
14,080 KB
testcase_22 AC 216 ms
13,056 KB
testcase_23 AC 188 ms
14,976 KB
testcase_24 AC 248 ms
15,104 KB
testcase_25 AC 276 ms
14,976 KB
testcase_26 AC 269 ms
14,976 KB
testcase_27 AC 272 ms
15,104 KB
testcase_28 AC 266 ms
14,976 KB
testcase_29 AC 309 ms
16,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <tuple>
using namespace std;
typedef tuple<long long, int, int> P;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int main(){
    int N, M;
    cin >> N >> M;
    vector<int> h(M), w(M);
    vector<long long> c(M);
    vector<vector<long long>> cost(N, vector<long long> (N, 1));
    for(int i = 0; i < M; i++) {
        cin >> h[i] >> w[i] >> c[i];
        cost[h[i] - 1][w[i] - 1] += c[i];
    }
    long long INF = 1e18;
    vector<vector<long long>> d(2, vector<long long>(N * N, INF));
    priority_queue<P, vector<P>, greater<P>> q;
    d[0][0] = 0;
    q.push(P(0, 0, 0));
    while(!q.empty()){
        P v = q.top();
        q.pop();
        long long temp = get<0>(v);
        int s = get<1>(v), t = get<2>(v);
        if(d[s][t] < temp) continue;
        if(s == 0 && cost[t / N][t % N] > 1){
            if(d[1][t] > d[0][t] - cost[t / N][t % N] + 1){
                d[1][t] = d[0][t] - cost[t / N][t % N] + 1;
                q.push(P(d[1][t], 1, t));
            }
        }
        for(int i = 0; i < 4; i++){
            int x = t / N, y = t % N;
            int nx = x + dx[i], ny = y + dy[i];
            if(0 <= nx && nx < N && 0 <= ny && ny < N){
                int nt = nx * N + ny;
                if(d[s][nt] > d[s][t] + cost[nx][ny]){
                    d[s][nt] = d[s][t] + cost[nx][ny];
                    q.push(P(d[s][nt], s, nt));
                }
            }
        }
    }
    cout << min(d[1][N * N - 1], d[0][N * N - 1]) << endl;
}
0