結果

問題 No.1283 Extra Fee
ユーザー kk
提出日時 2020-12-25 16:01:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,495 bytes
コンパイル時間 2,028 ms
コンパイル使用メモリ 219,100 KB
実行使用メモリ 12,148 KB
最終ジャッジ日時 2024-04-27 23:45:27
合計ジャッジ時間 5,059 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 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,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 9 ms
6,948 KB
testcase_12 AC 9 ms
6,940 KB
testcase_13 AC 7 ms
6,940 KB
testcase_14 AC 25 ms
6,940 KB
testcase_15 AC 39 ms
6,948 KB
testcase_16 AC 10 ms
6,940 KB
testcase_17 AC 122 ms
12,008 KB
testcase_18 AC 133 ms
8,960 KB
testcase_19 AC 143 ms
8,968 KB
testcase_20 AC 127 ms
8,832 KB
testcase_21 AC 132 ms
8,776 KB
testcase_22 AC 119 ms
8,476 KB
testcase_23 AC 119 ms
9,132 KB
testcase_24 AC 127 ms
9,152 KB
testcase_25 AC 145 ms
9,216 KB
testcase_26 AC 138 ms
9,088 KB
testcase_27 AC 140 ms
9,308 KB
testcase_28 AC 140 ms
9,256 KB
testcase_29 AC 134 ms
12,148 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