結果

問題 No.34 砂漠の行商人
ユーザー togatogatogatoga
提出日時 2015-09-12 14:51:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,963 ms / 5,000 ms
コード長 2,016 bytes
コンパイル時間 1,772 ms
コンパイル使用メモリ 153,216 KB
実行使用メモリ 397,036 KB
最終ジャッジ日時 2023-09-10 19:15:52
合計ジャッジ時間 15,129 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 212 ms
395,064 KB
testcase_01 AC 212 ms
395,308 KB
testcase_02 AC 213 ms
395,060 KB
testcase_03 AC 212 ms
395,020 KB
testcase_04 AC 243 ms
395,316 KB
testcase_05 AC 179 ms
395,348 KB
testcase_06 AC 160 ms
395,196 KB
testcase_07 AC 186 ms
395,404 KB
testcase_08 AC 198 ms
395,552 KB
testcase_09 AC 702 ms
396,824 KB
testcase_10 AC 204 ms
395,740 KB
testcase_11 AC 1,593 ms
396,956 KB
testcase_12 AC 169 ms
395,324 KB
testcase_13 AC 1,963 ms
397,036 KB
testcase_14 AC 1,519 ms
396,804 KB
testcase_15 AC 162 ms
395,364 KB
testcase_16 AC 233 ms
395,524 KB
testcase_17 AC 161 ms
395,432 KB
testcase_18 AC 164 ms
395,172 KB
testcase_19 AC 561 ms
396,964 KB
testcase_20 AC 909 ms
396,984 KB
testcase_21 AC 161 ms
395,528 KB
testcase_22 AC 169 ms
395,628 KB
testcase_23 AC 162 ms
395,384 KB
testcase_24 AC 1,150 ms
396,784 KB
testcase_25 AC 199 ms
395,728 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){
      ans = min(ans, (long long)pos.cost);
      continue;
    }
    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]));
	}
      }
    }
  }
  if (ans == 1LL << 40){
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}
0