結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  motxx | 
| 提出日時 | 2014-10-06 00:46:05 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3,295 ms / 5,000 ms | 
| コード長 | 1,451 bytes | 
| コンパイル時間 | 1,376 ms | 
| コンパイル使用メモリ | 163,840 KB | 
| 実行使用メモリ | 479,692 KB | 
| 最終ジャッジ日時 | 2024-06-28 10:04:22 | 
| 合計ジャッジ時間 | 23,591 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
#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;
}
            
            
            
        