結果

問題 No.1283 Extra Fee
ユーザー SSRS
提出日時 2020-11-06 21:45:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 450 ms / 2,000 ms
コード長 1,246 bytes
コンパイル時間 1,912 ms
コンパイル使用メモリ 189,216 KB
実行使用メモリ 31,184 KB
最終ジャッジ日時 2024-11-16 06:37:02
合計ジャッジ時間 8,441 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
vector<int> dy = {1, 0, -1, 0};
vector<int> dx = {0, 1, 0, -1};
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(N, 0));
  for (int i = 0; i < M; i++){
    int h, w, c;
    cin >> h >> w >> c;
    h--;
    w--;
    A[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>>> pq;
  vector<vector<vector<long long>>> cost(N, vector<vector<long long>>(N, vector<long long>(2, -1)));
  pq.push(make_tuple(0, 0, 0, 0));
  while (!pq.empty()){
    long long sum = get<0>(pq.top());
    int y = get<1>(pq.top());
    int x = get<2>(pq.top());
    int u = get<3>(pq.top());
    pq.pop();
    if (cost[y][x][u] == -1){
      cost[y][x][u] = sum;
      for (int i = 0; i < 4; i++){
        int y2 = y + dy[i];
        int x2 = x + dx[i];
        if (0 <= y2 && y2 < N && 0 <= x2 && x2 < N){
          if (cost[y2][x2][u] == -1){
            pq.push(make_tuple(sum + A[y2][x2] + 1, y2, x2, u));
          }
          if (u == 0 && cost[y2][x2][1] == -1){
            pq.push(make_tuple(sum + 1, y2, x2, 1));
          }
        }
      }
    }
  }
  cout << cost[N - 1][N - 1][1] << endl;
}
0