結果

問題 No.34 砂漠の行商人
ユーザー koba-e964koba-e964
提出日時 2015-06-11 21:18:46
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,832 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 94,916 KB
実行使用メモリ 91,316 KB
最終ジャッジ日時 2023-09-20 20:57:16
合計ジャッジ時間 17,693 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
5,708 KB
testcase_02 AC 9 ms
18,328 KB
testcase_03 AC 21 ms
16,356 KB
testcase_04 AC 117 ms
40,972 KB
testcase_05 AC 120 ms
47,996 KB
testcase_06 AC 16 ms
37,188 KB
testcase_07 AC 193 ms
58,620 KB
testcase_08 AC 263 ms
69,348 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 122 ms
44,084 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 TLE -
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3,174 ms
85,984 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PI;
const double EPS=1e-9;
const int N = 100;
const int V = 2000;
int n, v, sx,sy ,gx, gy;
int l[N][N];
int dp[N][N][V];
const int inf = 10000000;

int main(void){
  cin >> n >> v >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  REP(i, 0, n) {
    REP(j, 0, n) {
      cin >> l[i][j];
    }
  }
  if (v >= V) {
    cout << abs(sx - gx) + abs(sy - gy) << endl;
    return 0;
  }
  REP(i, 0, n) {
    REP(j, 0, n) {
      fill_n(dp[i][j], V, inf);
    }
  }
  typedef pair<int, PI> qtt;
  priority_queue<qtt, vector<qtt>, greater<qtt> > que;
  que.push(qtt(0, PI(sy * n + sx, v)));
  while (!que.empty()) {
    qtt p = que.top(); que.pop();
    int x = p.second.first;
    int y = x % n;
    int hp = p.second.second;
    x /= n;
    if (dp[x][y][hp] <= p.first) {
      continue;
    }
    dp[x][y][hp] = p.first;
    int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
    REP(d, 0, 4) {
      int nx = x + dx[d];
      int ny = y + dy[d];
      if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
	continue;
      }
      int newhp = hp - l[nx][ny];
      if (newhp <= 0) {
	continue;
      }
      que.push(qtt(p.first + 1, PI(nx * n + ny, newhp)));
    }
  }
  int mi = inf;
  REP(i, 0, V) {
    mi = min(mi, dp[gy][gx][i]);
  }
  cout << (mi == inf ? -1 : mi) << endl;
}
0