結果

問題 No.34 砂漠の行商人
ユーザー motxxmotxx
提出日時 2014-10-06 00:46:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,993 ms / 5,000 ms
コード長 1,451 bytes
コンパイル時間 1,458 ms
コンパイル使用メモリ 149,852 KB
実行使用メモリ 479,540 KB
最終ジャッジ日時 2023-09-10 18:54:18
合計ジャッジ時間 17,881 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 153 ms
476,576 KB
testcase_01 AC 153 ms
476,568 KB
testcase_02 AC 155 ms
476,620 KB
testcase_03 AC 155 ms
476,580 KB
testcase_04 AC 181 ms
476,680 KB
testcase_05 AC 179 ms
476,680 KB
testcase_06 AC 155 ms
476,548 KB
testcase_07 AC 196 ms
476,808 KB
testcase_08 AC 213 ms
476,952 KB
testcase_09 AC 982 ms
477,892 KB
testcase_10 AC 211 ms
477,028 KB
testcase_11 AC 2,098 ms
479,340 KB
testcase_12 AC 170 ms
476,912 KB
testcase_13 AC 2,993 ms
479,540 KB
testcase_14 AC 2,256 ms
479,400 KB
testcase_15 AC 155 ms
476,732 KB
testcase_16 AC 263 ms
477,152 KB
testcase_17 AC 152 ms
476,600 KB
testcase_18 AC 160 ms
476,644 KB
testcase_19 AC 744 ms
479,468 KB
testcase_20 AC 1,272 ms
479,392 KB
testcase_21 AC 156 ms
476,988 KB
testcase_22 AC 165 ms
477,012 KB
testcase_23 AC 157 ms
476,684 KB
testcase_24 AC 1,664 ms
479,464 KB
testcase_25 AC 219 ms
477,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

struct State {
  int hp;
  ll cost;
  int x, y;
  bool operator < (const State& rhs) const {
    if(cost != rhs.cost) return cost > rhs.cost;
    return hp < rhs.hp;
  }
};

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

int const INF = 1<<29;

int N;

bool valid(int x, int y) {
  return 0<=x && x<N && 0<=y && y<N;
}

ll L[110][110];
int cdist[110][110][10010];

int main () {
  
  int V, sx, sy, gx, gy;
  cin >> N >> V >> sx >> sy >> gx >> gy;
  sx --, sy --, gx --, gy --;
  
  rep(i, N) {
    rep(j, N) {
      cin >> L[i][j]; 
    }
  }
  
  priority_queue<State> pq;
  pq.push((State){V, 0, sx, sy});
  
  ll ans = INF;
  
  fill(cdist[0][0], cdist[110][0], INF);
  cdist[sy][sx][0] = 0;
  while(!pq.empty()) {
    int hp = pq.top().hp;
    ll cost = pq.top().cost;
    int x = pq.top().x, y = pq.top().y; pq.pop();
    
    if(gx == x && gy == y) {
      ans = cost;
      break;
    }
    
    rep(i, 4) {
      int nx = x+dx[i], ny = y+dy[i];
      if(!valid(nx, ny)) continue;
      int nhp = hp-L[ny][nx];
      if(nhp <= 0) { continue; }
      if(cdist[ny][nx][nhp] <= cost+1) { continue; }
      cdist[ny][nx][nhp] = cost+1;
      pq.push((State){nhp, cdist[ny][nx][nhp], nx, ny});
    }
    
  }
  
  if(ans != INF) cout << ans << endl;
  else cout << -1 << endl;
  
  return 0;
}
0