結果
| 問題 |
No.34 砂漠の行商人
|
| コンテスト | |
| ユーザー |
togatoga
|
| 提出日時 | 2015-09-12 14:58:21 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2,485 ms / 5,000 ms |
| コード長 | 1,943 bytes |
| コンパイル時間 | 1,495 ms |
| コンパイル使用メモリ | 166,496 KB |
| 実行使用メモリ | 397,076 KB |
| 最終ジャッジ日時 | 2024-06-28 10:24:50 |
| 合計ジャッジ時間 | 19,707 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#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 or ans < pos.cost){
continue;
}
if (pos.node == gy * N + gx){
cout << pos.cost << endl;
return 0;
}
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]));
}
}
}
}
cout << -1 << endl;
return 0;
}
togatoga