結果

問題 No.34 砂漠の行商人
ユーザー mint6421mint6421
提出日時 2019-11-21 00:22:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,712 bytes
コンパイル時間 1,749 ms
コンパイル使用メモリ 171,012 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-04-16 21:10:01
合計ジャッジ時間 8,493 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 18 ms
6,912 KB
testcase_05 AC 16 ms
7,424 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 27 ms
9,432 KB
testcase_08 AC 34 ms
13,312 KB
testcase_09 TLE -
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<bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int,P>
#define PI pair<P,int>
#define PP pair<P,P>
#define Yes(f){cout<<(f?"Yes":"No")<<endl;}
#define YES(f){cout<<(f?"YES":"NO")<<endl;}
#define MPlus(x,y) {x=(x+y)%M;}
#define MSub(x,y) {x=(x-y+M)%M;}
#define MMul(x,y) {x=(x*y)%M;}


signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(20);

  int n,v,sx,sy,gx,gy;
  cin>>n>>v>>sx>>sy>>gx>>gy;
  sx--;sy--;gx--;gy--;
  int d[n][n];
  rep(i,n){
    rep(j,n){
      cin>>d[i][j];
    }
  }
  int dp[n][n][v+1];
  rep(i,n){
    rep(j,n){
      rep(k,v+1){
        dp[i][j][k]=inf;
      }
    }
  }
  dp[sy][sx][v]=0;

  queue<P> vec[v+1];
  vec[v].push(P(sx,sy));

  rrep(k,v){
    while(vec[k].size()){
      P p=vec[k].front(); vec[k].pop();
      rep(i,4){
        int x=p.F+vx[i],y=p.S+vy[i];
        if(!(0<=x&&x<n&&0<=y&&y<n)) continue;
        if(k-d[y][x]<=0) continue;
        if(dp[y][x][k-d[y][x]]>dp[p.S][p.F][k]+1){
          dp[y][x][k-d[y][x]]=dp[p.S][p.F][k]+1;
          vec[k-d[y][x]].push(P(x,y));
        }
      }
    }
  }
  
  int ans=inf;
  FOR(i,1,v+1){
    ans=min(ans,dp[gy][gx][i]);
  }

  cout<<(ans==inf?-1:ans)<<endl;


}
0