結果

問題 No.34 砂漠の行商人
ユーザー togatogatogatoga
提出日時 2015-09-12 14:50:12
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,000 bytes
コンパイル時間 2,250 ms
コンパイル使用メモリ 152,800 KB
実行使用メモリ 791,680 KB
最終ジャッジ日時 2023-09-26 11:39:56
合計ジャッジ時間 12,167 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 AC 264 ms
394,956 KB
testcase_02 AC 209 ms
395,044 KB
testcase_03 AC 207 ms
395,068 KB
testcase_04 AC 220 ms
395,152 KB
testcase_05 AC 222 ms
395,296 KB
testcase_06 AC 208 ms
395,116 KB
testcase_07 AC 232 ms
395,284 KB
testcase_08 AC 244 ms
395,524 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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){

      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