結果

問題 No.1283 Extra Fee
ユーザー siman
提出日時 2020-12-25 11:44:10
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 510 ms / 2,000 ms
コード長 1,690 bytes
コンパイル時間 3,515 ms
コンパイル使用メモリ 141,932 KB
実行使用メモリ 18,656 KB
最終ジャッジ日時 2024-11-16 07:16:27
合計ジャッジ時間 10,378 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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