結果
| 問題 |
No.34 砂漠の行商人
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-07-10 17:17:04 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,476 bytes |
| コンパイル時間 | 2,305 ms |
| コンパイル使用メモリ | 208,592 KB |
| 最終ジャッジ日時 | 2025-01-30 06:11:43 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 TLE * 7 |
ソースコード
#include<bits/stdc++.h>
//#include<atcoder/mincostflow>
using namespace std;
//using namespace atcoder;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void solve(){
int n, v, sy, sx, gy, gx;
cin >> n >> v >> sy >> sx >> gy >> gx;
vector<vector<int>> L(n, vector<int>(n));
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) cin >> L[i][j];
sx--; sy--; gx--; gy--;
auto f = [&](int i, int j, int k) -> int{
return (i * n + j) * v + k;
};
map<int, int> dist;
dist[f(sx, sy, v - 1)] = 0;
queue<int> que;
que.push(f(sx, sy, v - 1));
while(!que.empty()){
int tmp = que.front();
que.pop();
int x = tmp / (n * v);
tmp -= x * n * v;
int y = tmp / v;
int vv = tmp - y * v;
for(int t = 0; t < 4; t++){
int nx = x + dx[t];
int ny = y + dy[t];
if(nx == -1 || nx == n || ny == -1 || ny == n) continue;
int nv = vv - L[nx][ny];
if(nv < 0) continue;
if(!dist.count(f(nx, ny, nv))){
if(nx == gx && ny == gy){
cout << dist[f(x, y, vv)] + 1 << "\n";
return;
}
dist[f(nx, ny, nv)] = dist[f(x, y, vv)] + 1;
que.push(f(nx, ny, nv));
}
}
}
cout << "-1\n";
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int t;
t = 1;
//cin >> t;
while(t--) solve();
}