結果

問題 No.34 砂漠の行商人
ユーザー motxxmotxx
提出日時 2014-10-06 00:43:11
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,447 bytes
コンパイル時間 3,108 ms
コンパイル使用メモリ 156,764 KB
実行使用メモリ 270,856 KB
最終ジャッジ日時 2023-08-28 20:10:43
合計ジャッジ時間 15,212 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 5 ms
4,524 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 112 ms
14,520 KB
testcase_05 AC 102 ms
14,732 KB
testcase_06 AC 5 ms
4,588 KB
testcase_07 AC 180 ms
21,784 KB
testcase_08 AC 263 ms
28,100 KB
testcase_09 TLE -
testcase_10 AC 223 ms
27,092 KB
testcase_11 TLE -
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;

#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];
map<ll, int> cdist[110][110];

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;
  
  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].find(nhp) != cdist[ny][nx].end() &&
         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