結果

問題 No.1283 Extra Fee
ユーザー simansiman
提出日時 2020-12-25 11:44:10
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 560 ms / 2,000 ms
コード長 1,690 bytes
コンパイル時間 1,424 ms
コンパイル使用メモリ 143,056 KB
実行使用メモリ 18,388 KB
最終ジャッジ日時 2024-04-27 23:45:39
合計ジャッジ時間 9,290 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,820 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 15 ms
6,940 KB
testcase_12 AC 28 ms
6,940 KB
testcase_13 AC 22 ms
6,944 KB
testcase_14 AC 89 ms
6,940 KB
testcase_15 AC 142 ms
6,944 KB
testcase_16 AC 30 ms
6,940 KB
testcase_17 AC 232 ms
18,388 KB
testcase_18 AC 506 ms
6,944 KB
testcase_19 AC 540 ms
6,944 KB
testcase_20 AC 507 ms
6,944 KB
testcase_21 AC 513 ms
6,940 KB
testcase_22 AC 455 ms
6,940 KB
testcase_23 AC 406 ms
6,940 KB
testcase_24 AC 480 ms
6,940 KB
testcase_25 AC 555 ms
6,940 KB
testcase_26 AC 559 ms
6,944 KB
testcase_27 AC 560 ms
6,940 KB
testcase_28 AC 559 ms
6,944 KB
testcase_29 AC 253 ms
18,268 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;

const int MAX_N = 500;
ll costs[MAX_N][MAX_N];
const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};

struct Node {
  int y;
  int x;
  ll cost;
  bool canIgnoreFee;

  Node(int y = -1, int x = -1, ll cost = -1, bool canIgnoreFee = true) {
    this->y = y;
    this->x = x;
    this->cost = cost;
    this->canIgnoreFee = canIgnoreFee;
  }

  bool operator>(const Node &n) const {
    return cost > n.cost;
  }
};

int main() {
  memset(costs, 0, sizeof(costs));

  int N, M;
  cin >> N >> M;

  ll h, w, c;
  for (int i = 0; i < M; ++i) {
    cin >> h >> w >> c;

    costs[h - 1][w - 1] = c;
  }

  priority_queue <Node, vector<Node>, greater<Node>> pque;
  pque.push(Node(0, 0, 0, true));
  bool visited[2][N][N];
  memset(visited, false, sizeof(visited));

  while (!pque.empty()) {
    Node node = pque.top();
    pque.pop();

    if (visited[node.canIgnoreFee][node.y][node.x]) continue;
    visited[node.canIgnoreFee][node.y][node.x] = true;

    if (node.y == N - 1 && node.x == N - 1) {
      cout << node.cost << endl;
      return 0;
    }

    for (int i = 0; i < 4; ++i) {
      int ny = node.y + DY[i];
      int nx = node.x + DX[i];
      if (ny < 0 || nx < 0 || N <= ny || N <= nx) continue;

      pque.push(Node(ny, nx, node.cost + costs[ny][nx] + 1, node.canIgnoreFee));

      if (costs[ny][nx] != 0 && node.canIgnoreFee) {
        pque.push(Node(ny, nx, node.cost + 1, false));
      }
    }
  }

  return 0;
}
0