結果

問題 No.34 砂漠の行商人
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-05-21 23:30:44
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,437 bytes
コンパイル時間 1,797 ms
コンパイル使用メモリ 184,856 KB
実行使用メモリ 799,776 KB
最終ジャッジ日時 2024-04-10 07:33:51
合計ジャッジ時間 13,879 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 11 ms
6,940 KB
testcase_05 AC 11 ms
7,168 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 18 ms
8,960 KB
testcase_08 AC 24 ms
12,672 KB
testcase_09 TLE -
testcase_10 MLE -
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 #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.05.21 23:03:57
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int n, v;
vector< vector< int > > field;
const int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
// dijkstra O(ElogV)
// verify : https://onlinejudge.u-aizu.ac.jp/problems/GRL_1_A
// ※拡張ダイクストラ
template < typename T >
struct Dijkstra {
private:
    int N, V;
public:
    const T inf = numeric_limits< T >::max();
    // s から i の最小コスト
    // 経路がない場合は inf
    vector< vector< vector< T > > > d;  // (頂点x,頂点y,残り) ※
    Dijkstra(int N, int V) : N(N), V(V) {}
    void build(int sx, int sy) {
        d.assign(N, vector< vector< T > >(N, vector< T >(V + 1, inf)));  // ※
        typedef tuple< T, int, int, int > P;                             //(距離,頂点x,頂点y,残り) ※
        queue< P > pq;
        d[sx][sy][V] = 0;                     // ※
        pq.push(P(d[sx][sy][V], sx, sy, V));  // ※
        while (!pq.empty()) {
            P p = pq.front();
            pq.pop();
            int vx = get< 1 >(p);
            int vy = get< 2 >(p);
            int rest = get< 3 >(p);
            // ※
            if (d[vx][vy][rest] < get< 0 >(p)) continue;  // ※
            // ※
            for (int k = 0; k < 4; k++) {
                int x = vx + dx[k], y = vy + dy[k];
                if (x >= 0 && x < n && y >= 0 && y < n && rest - field[x][y] > 0 && d[x][y][rest - field[x][y]] > d[vx][vy][rest] + 1) {
                    d[x][y][rest - field[x][y]] = d[vx][vy][rest] + 1;
                    pq.push(P(d[x][y][rest - field[x][y]], x, y, rest - field[x][y]));
                    //cout << d[x][y][rest - field[x][y]] << " " << x << " " << y << " " << rest - field[x][y] << endl;
                }
            }
        }
    }
};
int main() {
    cin >> n >> v;
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    field.resize(n,vector<int>(n));
    Dijkstra< ll > g(n, v);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> field[i][j];
        }
    }
    g.build(sy - 1, sx - 1);
    ll ans = g.inf;
    for (int i = 0; i < v; i++) {
        ans = min(ans, g.d[gy - 1][gx - 1][i + 1]);
        //cout << i + 1 << " " << ans << endl;
    }
    if (ans == g.inf) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
}
0