結果

問題 No.34 砂漠の行商人
ユーザー 0w10w1
提出日時 2016-12-10 22:47:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 987 ms / 5,000 ms
コード長 1,883 bytes
コンパイル時間 1,818 ms
コンパイル使用メモリ 178,696 KB
実行使用メモリ 75,628 KB
最終ジャッジ日時 2023-09-10 19:26:20
合計ジャッジ時間 6,828 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
73,720 KB
testcase_01 AC 21 ms
73,700 KB
testcase_02 AC 21 ms
73,736 KB
testcase_03 AC 21 ms
73,684 KB
testcase_04 AC 29 ms
73,812 KB
testcase_05 AC 29 ms
73,780 KB
testcase_06 AC 21 ms
73,720 KB
testcase_07 AC 35 ms
73,860 KB
testcase_08 AC 39 ms
73,960 KB
testcase_09 AC 275 ms
74,748 KB
testcase_10 AC 34 ms
74,068 KB
testcase_11 AC 386 ms
75,220 KB
testcase_12 AC 27 ms
73,956 KB
testcase_13 AC 987 ms
75,628 KB
testcase_14 AC 753 ms
75,504 KB
testcase_15 AC 22 ms
73,892 KB
testcase_16 AC 58 ms
74,228 KB
testcase_17 AC 21 ms
73,740 KB
testcase_18 AC 23 ms
73,800 KB
testcase_19 AC 206 ms
75,288 KB
testcase_20 AC 372 ms
75,540 KB
testcase_21 AC 22 ms
73,808 KB
testcase_22 AC 26 ms
74,168 KB
testcase_23 AC 22 ms
73,772 KB
testcase_24 AC 469 ms
75,424 KB
testcase_25 AC 41 ms
74,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int N, V, Sx, Sy, Gx, Gy;
vvi LV;

void init(){
  cin >> N >> V >> Sx >> Sy >> Gx >> Gy;
  upmin( V, 1800 - 1 );
  --Sx, --Sy, --Gx, --Gy;
  swap( Sx, Sy ), swap( Gx, Gy );
  LV = vvi( N, vi( N ) );
  for( int i = 0; i < N; ++i )
    for( int j = 0; j < N; ++j )
      cin >> LV[ i ][ j ];
}

int dis[ 1800 ][ 100 ][ 100 ];

void preprocess(){
  memset( dis, INF, sizeof( dis ) );
  typedef tuple< int, int, int, int > t4i;
  queue< t4i > que;
  que.emplace( dis[ V ][ Sx ][ Sy ] = 0, V, Sx, Sy );
  while( not que.empty() ){
    int d, v, x, y; tie( d, v, x, y ) = que.front(); que.pop();
    if( dis[ v ][ x ][ y ] != d ) continue;
    if( x == Gx and y == Gy )
      cout << d << endl, exit( 0 );
    const int dx[] = { 0, 1, 0, -1 };
    const int dy[] = { 1, 0, -1, 0 };
    for( int di = 0; di < 4; ++di ){
      int nx = x + dx[ di ];
      int ny = y + dy[ di ];
      if( not ( 0 <= nx and nx < N and 0 <= ny and ny < N ) ) continue;
      if( v - LV[ nx ][ ny ] <= 0 ) continue;
      if( upmin( dis[ v - LV[ nx ][ ny ] ][ nx ][ ny ], dis[ v ][ x ][ y ] + 1 ) )
        que.emplace( dis[ v - LV[ nx ][ ny ] ][ nx ][ ny ], v - LV[ nx ][ ny ], nx, ny );
    }
  }
  cout << -1 << endl;
}

void solve(){

}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0