結果

問題 No.496 ワープクリスタル (給料日前編)
ユーザー simansiman
提出日時 2021-09-08 15:23:08
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 2,007 bytes
コンパイル時間 3,630 ms
コンパイル使用メモリ 104,072 KB
実行使用メモリ 5,580 KB
最終ジャッジ日時 2023-08-26 22:04:07
合計ジャッジ時間 5,114 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 5 ms
5,508 KB
testcase_09 AC 5 ms
5,528 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,504 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 5 ms
5,528 KB
testcase_23 AC 6 ms
5,528 KB
testcase_24 AC 5 ms
5,580 KB
testcase_25 AC 5 ms
5,352 KB
testcase_26 AC 5 ms
5,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

struct Crystal {
  int y;
  int x;
  int c;

  Crystal(int y = -1, int x = -1, int c = -1) {
    this->y = y;
    this->x = x;
    this->c = c;
  }
};

int main() {
  int GX, GY, N, F;
  cin >> GX >> GY >> N >> F;
  vector<Crystal> crystals;

  int x, y, c;
  for (int i = 0; i < N; ++i) {
    cin >> x >> y >> c;

    crystals.push_back(Crystal(y, x, c));
  }

  int dp[N + 1][GY + 1][GX + 1];
  memset(dp, -1, sizeof(dp));

  for (int y = 0; y <= GY; ++y) {
    for (int x = 0; x <= GX; ++x) {
      int dist = y + x;
      dp[0][y][x] = F * dist;
    }
  }

  for (int i = 0; i < N; ++i) {
    Crystal crystal = crystals[i];

    for (int y = 0; y <= GY; ++y) {
      for (int x = 0; x <= GX; ++x) {
        int ny = y + crystal.y;
        int nx = x + crystal.x;
        if (dp[i + 1][y][x] == -1) {
          dp[i + 1][y][x] = dp[i][y][x];
        } else {
          dp[i + 1][y][x] = min(dp[i + 1][y][x], dp[i][y][x]);
        }

        if (0 <= ny && ny <= GY && 0 <= nx && nx <= GX) {
          if (dp[i + 1][ny][nx] == -1) {
            dp[i + 1][ny][nx] = dp[i][y][x] + crystal.c;
          } else {
            dp[i + 1][ny][nx] = min(dp[i + 1][ny][nx], dp[i][y][x] + crystal.c);
          }
        }

        if (y + 1 <= GY) {
          if (dp[i + 1][y + 1][x] == -1) {
            dp[i + 1][y + 1][x] = dp[i][y][x] + F;
          } else {
            dp[i + 1][y + 1][x] = min(dp[i + 1][y + 1][x], dp[i][y][x] + F);
          }
        }

        if (x + 1 <= GX) {
          if (dp[i + 1][y][x + 1] == -1) {
            dp[i + 1][y][x + 1] = dp[i][y][x] + F;
          } else {
            dp[i + 1][y][x + 1] = min(dp[i + 1][y][x + 1], dp[i][y][x] + F);
          }
        }
      }
    }
  }

  cout << dp[N][GY][GX] << endl;

  return 0;
}
0