結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,528 KB
testcase_01 AC 4 ms
8,776 KB
testcase_02 AC 17 ms
9,216 KB
testcase_03 AC 32 ms
10,484 KB
testcase_04 AC 367 ms
19,840 KB
testcase_05 AC 354 ms
19,936 KB
testcase_06 AC 26 ms
9,528 KB
testcase_07 AC 633 ms
26,752 KB
testcase_08 AC 910 ms
33,400 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;}

int n,v,sx,sy,gx,gy;
int d[110][110];
int dp[100][100];

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

  
  cin>>n>>v>>sx>>sy>>gx>>gy;
  sx--;sy--;gx--;gy--;
  
  rep(i,n){
    rep(j,n){
      cin>>d[i][j];
    }
  }
  map<P,int> dp[110000];
  dp[v][P(sx,sy)]=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();
      //cout<<p.F<<' '<<p.S<<endl;
      rep(l,4){
        int x=p.F+vx[l];
        int y=p.S+vy[l];
        //cout<<' '<<x<<' '<<y<<endl;
        if(!(0<=x&&x<n&&0<=y&&y<n)) continue;
        if(k-d[y][x]<=0) continue;
        
        if(dp[k-d[y][x]].find(P(x,y))==dp[k-d[y][x]].end()||dp[k-d[y][x]][P(x,y)]>dp[k][p]+1){
          dp[k-d[y][x]][P(x,y)]=dp[k][p]+1;
          vec[k-d[y][x]].push(P(x,y));
        }
      }
    }
  }

  int ans=inf;
  FOR(i,1,v+1){
    if(dp[i].find(P(gx,gy))==dp[i].end()) continue;
    ans=min(ans,dp[i][P(gx,gy)]);
  }

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