結果

問題 No.34 砂漠の行商人
ユーザー togatogatogatoga
提出日時 2015-09-12 14:58:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,942 ms / 5,000 ms
コード長 1,943 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 153,024 KB
実行使用メモリ 397,032 KB
最終ジャッジ日時 2023-09-10 19:16:43
合計ジャッジ時間 13,307 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
394,992 KB
testcase_01 AC 129 ms
395,116 KB
testcase_02 AC 131 ms
395,116 KB
testcase_03 AC 129 ms
395,024 KB
testcase_04 AC 143 ms
395,276 KB
testcase_05 AC 144 ms
395,368 KB
testcase_06 AC 129 ms
395,120 KB
testcase_07 AC 156 ms
395,412 KB
testcase_08 AC 166 ms
395,492 KB
testcase_09 AC 654 ms
396,804 KB
testcase_10 AC 170 ms
395,848 KB
testcase_11 AC 1,575 ms
396,952 KB
testcase_12 AC 139 ms
395,276 KB
testcase_13 AC 1,942 ms
397,032 KB
testcase_14 AC 1,509 ms
396,988 KB
testcase_15 AC 132 ms
395,304 KB
testcase_16 AC 199 ms
395,436 KB
testcase_17 AC 131 ms
395,444 KB
testcase_18 AC 134 ms
395,168 KB
testcase_19 AC 526 ms
396,788 KB
testcase_20 AC 861 ms
396,796 KB
testcase_21 AC 132 ms
395,568 KB
testcase_22 AC 138 ms
395,624 KB
testcase_23 AC 130 ms
395,312 KB
testcase_24 AC 1,093 ms
396,840 KB
testcase_25 AC 168 ms
395,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;


const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

struct Edge{
  int node,cost;
  Edge(int node, int cost):node(node),cost(cost){
  }
};

struct State{
  int hp,node;
  int cost;
  State(int node, int hp, int cost):hp(hp),node(node),cost(cost){}
  bool operator < (const State& right) const {
    return cost > right.cost;
  }
};

int N,V,sx,sy,gx,gy;
int board[110][110];
int cost[10010][10010];
vector<Edge> Edges[10010];
int main(){
  cin >> N >> V >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      cin >> board[i][j];
    }
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      int ny,nx;
      int node = i * N + j;
      for (int k = 0; k < 4; k++){
	ny = i + dy[k];
	nx = j + dx[k];
	if (ny < 0 or nx < 0 or ny >= N or nx >= N)continue;
	int next_node = ny * N + nx;
	int cost = board[ny][nx];
	Edges[node].push_back(Edge(next_node, cost));
      }
    }
  }
  priority_queue<State> que;
  que.push(State(sy * N + sx, V, false));
  for (int i = 0; i < 10010; i++){
    for (int j = 0; j < 10000; j++){
      cost[i][j] = 1LL << 30;
    }
  }
  cost[sy * N + sx][V] = 0;
  long long ans = 1LL << 40;
  while (!que.empty()){
    State pos = que.top();
    que.pop();
    //cout << pos.node << " " << pos.hp << endl;
    if (cost[pos.node][pos.hp] > pos.cost or ans < pos.cost){
      continue;
    }
    if (pos.node == gy * N + gx){
      cout << pos.cost << endl;
      return 0;
    }
    for (int i = 0; i < Edges[pos.node].size(); i++){
      Edge np = Edges[pos.node][i];
      if (pos.hp - np.cost > 0){
	if (cost[np.node][pos.hp - np.cost] > cost[pos.node][pos.hp] + 1){
	  cost[np.node][pos.hp - np.cost] = cost[pos.node][pos.hp] + 1;
	  que.push(State(np.node, pos.hp - np.cost, cost[np.node][pos.hp - np.cost]));
	}
      }
    }
  }

  cout << -1 << endl;
    
  return 0;
}
0