結果

問題 No.1283 Extra Fee
ユーザー kk
提出日時 2020-12-25 16:01:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 1,495 bytes
コンパイル時間 2,343 ms
コンパイル使用メモリ 216,692 KB
実行使用メモリ 12,388 KB
最終ジャッジ日時 2023-08-10 06:27:42
合計ジャッジ時間 5,975 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,388 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 10 ms
4,380 KB
testcase_12 AC 11 ms
4,456 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 27 ms
5,204 KB
testcase_15 AC 43 ms
5,872 KB
testcase_16 AC 10 ms
4,524 KB
testcase_17 AC 140 ms
12,388 KB
testcase_18 AC 146 ms
8,880 KB
testcase_19 AC 158 ms
9,204 KB
testcase_20 AC 148 ms
8,864 KB
testcase_21 AC 150 ms
8,964 KB
testcase_22 AC 135 ms
8,432 KB
testcase_23 AC 130 ms
8,844 KB
testcase_24 AC 147 ms
8,864 KB
testcase_25 AC 167 ms
9,160 KB
testcase_26 AC 165 ms
9,324 KB
testcase_27 AC 160 ms
9,488 KB
testcase_28 AC 163 ms
9,196 KB
testcase_29 AC 151 ms
12,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

const long long INF = 1LL<<60;
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
long long dist[500][500][2];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < 2; k++)
        dist[i][j][k] = INF;
  
  vector<vector<long long> > cost(n, vector<long long>(n));

  for (int i = 0; i < m; i++) {
    int h, w, c;
    cin >> h >> w >> c;
    --h, --w;
    cost[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>> > q;
  dist[0][0][0] = 0;
  q.emplace(0, 0, 0, 0);

  while (!q.empty()) {
    long long d;
    int x, y, z;
    tie(d, y, x, z) = q.top();
    q.pop();

    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
      if (dist[y][x][z] + 1 + cost[ny][nx] < dist[ny][nx][z]) {
        dist[ny][nx][z] = dist[y][x][z] + 1 + cost[ny][nx];
        q.emplace(dist[ny][nx][z], ny, nx, z);
      }
      if (z == 0) {
        if (dist[y][x][z] + 1 < dist[ny][nx][z+1]) {
          dist[ny][nx][z+1] = dist[y][x][z] + 1;
          q.emplace(dist[ny][nx][z+1], ny, nx, z+1);
        }
      }
    }
  }
  
  cout << min(dist[n-1][n-1][0], dist[n-1][n-1][1]) << endl;
  
  return 0;
}
0