結果

問題 No.1283 Extra Fee
コンテスト
ユーザー siman
提出日時 2020-12-25 11:41:59
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,688 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 143,572 KB
実行使用メモリ 14,640 KB
最終ジャッジ日時 2024-09-22 04:43:26
合計ジャッジ時間 9,032 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19 WA * 11
権限があれば一括ダウンロードができます

ソースコード

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;
int 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;
  int cost;
  bool canIgnoreFee;

  Node(int y = -1, int x = -1, int 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;

  int 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));
  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