結果

問題 No.34 砂漠の行商人
ユーザー krotonkroton
提出日時 2014-10-18 20:30:43
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,702 bytes
コンパイル時間 831 ms
コンパイル使用メモリ 77,416 KB
実行使用メモリ 194,792 KB
最終ジャッジ日時 2023-08-28 21:19:24
合計ジャッジ時間 7,683 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,388 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 5 ms
7,064 KB
testcase_03 AC 2 ms
5,504 KB
testcase_04 AC 20 ms
14,400 KB
testcase_05 AC 28 ms
17,656 KB
testcase_06 AC 15 ms
12,360 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
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 <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <sstream>
using namespace std;
typedef long long ll;

const int INF = 1 << 20;

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

struct State {
    int x, y, d;
    bool operator>(const State& other) const {
        return d > other.d;
    }
};

int rest[100][100][10001];

int N, V, Sx, Sy, Gx, Gy;
int in[110][110];

int solve(){
    priority_queue<State, vector<State>, greater<State> > Q;
    Q.push(State{Sx, Sy, 0});
    
    rest[Sx][Sy][0] = V;
    
    int res = INF;
    while(!Q.empty()){
        State s = Q.top(); Q.pop();
        int x = s.x, y = s.y, d = s.d, v = rest[x][y][d];
        
        if(d >= res)continue;
        if(x == Gx && y == Gy){
            res = min(res, d);
            continue;
        }
        
        for(int i=0;i<4;i++){
            int nd = d + 1;
            
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 || ny >= N || nx < 0 || nx >= N)continue;
            
            int nv = v - in[nx][ny];
            if(nv <= rest[nx][ny][nd])continue;
            
            Q.push(State{nx, ny, nd});
            rest[nx][ny][nd] = nv;
        }
    }
    
    if(res >= INF){
        return -1;
    } else {
        return res;
    }
}

int main(){
    cin >> N >> V >> Sx >> Sy >> Gx >> Gy;
    
    --Sx; --Sy;
    --Gx; --Gy;
    
    for(int y=0;y<N;y++)for(int x=0;x<N;x++){
        cin >> in[x][y];
    }
    
    cout << solve() << endl;
    return 0;
}
0