結果

問題 No.1283 Extra Fee
ユーザー SSRSSSRS
提出日時 2020-11-06 21:45:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 417 ms / 2,000 ms
コード長 1,246 bytes
コンパイル時間 1,757 ms
コンパイル使用メモリ 189,796 KB
実行使用メモリ 31,368 KB
最終ジャッジ日時 2024-04-27 23:13:59
合計ジャッジ時間 7,828 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 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 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 17 ms
6,940 KB
testcase_12 AC 20 ms
6,944 KB
testcase_13 AC 13 ms
6,940 KB
testcase_14 AC 59 ms
6,940 KB
testcase_15 AC 96 ms
7,680 KB
testcase_16 AC 21 ms
6,940 KB
testcase_17 AC 332 ms
29,428 KB
testcase_18 AC 336 ms
17,280 KB
testcase_19 AC 357 ms
17,920 KB
testcase_20 AC 331 ms
16,896 KB
testcase_21 AC 352 ms
17,152 KB
testcase_22 AC 314 ms
15,616 KB
testcase_23 AC 359 ms
18,048 KB
testcase_24 AC 417 ms
18,048 KB
testcase_25 AC 384 ms
18,176 KB
testcase_26 AC 375 ms
18,304 KB
testcase_27 AC 378 ms
18,304 KB
testcase_28 AC 375 ms
18,304 KB
testcase_29 AC 385 ms
31,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
vector<int> dy = {1, 0, -1, 0};
vector<int> dx = {0, 1, 0, -1};
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(N, 0));
  for (int i = 0; i < M; i++){
    int h, w, c;
    cin >> h >> w >> c;
    h--;
    w--;
    A[h][w] = c;
  }
  priority_queue<tuple<long long, int, int, int>, vector<tuple<long long, int, int, int>>, greater<tuple<long long, int, int, int>>> pq;
  vector<vector<vector<long long>>> cost(N, vector<vector<long long>>(N, vector<long long>(2, -1)));
  pq.push(make_tuple(0, 0, 0, 0));
  while (!pq.empty()){
    long long sum = get<0>(pq.top());
    int y = get<1>(pq.top());
    int x = get<2>(pq.top());
    int u = get<3>(pq.top());
    pq.pop();
    if (cost[y][x][u] == -1){
      cost[y][x][u] = sum;
      for (int i = 0; i < 4; i++){
        int y2 = y + dy[i];
        int x2 = x + dx[i];
        if (0 <= y2 && y2 < N && 0 <= x2 && x2 < N){
          if (cost[y2][x2][u] == -1){
            pq.push(make_tuple(sum + A[y2][x2] + 1, y2, x2, u));
          }
          if (u == 0 && cost[y2][x2][1] == -1){
            pq.push(make_tuple(sum + 1, y2, x2, 1));
          }
        }
      }
    }
  }
  cout << cost[N - 1][N - 1][1] << endl;
}
0