結果

問題 No.34 砂漠の行商人
ユーザー 0w1
提出日時 2016-12-10 22:47:12
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,118 ms / 5,000 ms
コード長 1,883 bytes
コンパイル時間 1,777 ms
コンパイル使用メモリ 181,624 KB
実行使用メモリ 75,776 KB
最終ジャッジ日時 2024-06-28 10:34:06
合計ジャッジ時間 7,145 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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