結果
問題 | No.2328 Build Walls |
ユーザー | 👑 p-adic |
提出日時 | 2023-06-08 09:41:17 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,312 ms / 3,000 ms |
コード長 | 29,455 bytes |
コンパイル時間 | 3,928 ms |
コンパイル使用メモリ | 247,336 KB |
実行使用メモリ | 55,304 KB |
最終ジャッジ日時 | 2024-06-09 18:52:06 |
合計ジャッジ時間 | 14,249 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 31 ms
8,448 KB |
testcase_14 | AC | 362 ms
16,360 KB |
testcase_15 | AC | 186 ms
13,108 KB |
testcase_16 | AC | 24 ms
5,376 KB |
testcase_17 | AC | 147 ms
12,740 KB |
testcase_18 | AC | 4 ms
5,376 KB |
testcase_19 | AC | 5 ms
5,376 KB |
testcase_20 | AC | 13 ms
5,376 KB |
testcase_21 | AC | 22 ms
8,064 KB |
testcase_22 | AC | 614 ms
22,892 KB |
testcase_23 | AC | 973 ms
37,888 KB |
testcase_24 | AC | 788 ms
35,556 KB |
testcase_25 | AC | 900 ms
37,504 KB |
testcase_26 | AC | 535 ms
31,332 KB |
testcase_27 | AC | 710 ms
34,024 KB |
testcase_28 | AC | 42 ms
11,008 KB |
testcase_29 | AC | 961 ms
38,000 KB |
testcase_30 | AC | 51 ms
11,192 KB |
testcase_31 | AC | 47 ms
11,008 KB |
testcase_32 | AC | 693 ms
33,768 KB |
testcase_33 | AC | 1,312 ms
55,304 KB |
testcase_34 | AC | 58 ms
11,904 KB |
testcase_35 | AC | 1,025 ms
45,396 KB |
testcase_36 | AC | 2 ms
5,376 KB |
ソースコード
#ifdef DEBUG #define _GLIBCXX_DEBUG #define CERR( ANSWER ) cerr << ANSWER << endl; #define LIBRARY_SEARCH if( LibrarySearch() != 0 ){ QUIT; }; #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define CERR( ANSWER ) #define LIBRARY_SEARCH #endif #include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define TYPE_OF( VAR ) decay_t<decltype( VAR )> #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define CIN( LL , A ) LL A; cin >> A #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX ) #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #define GETLINE( A ) string A; getline( cin , A ) #define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR ) #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ ) #define FOREQINV( VAR , INITIAL , FINAL ) for( TYPE_OF( INITIAL ) VAR = INITIAL ; VAR >= FINAL ; VAR -- ) #define FOR_ITR( ARRAY , ITR , END ) for( auto ITR = ARRAY .begin() , END = ARRAY .end() ; ITR != END ; ITR ++ ) #define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES ) #define QUIT return 0 #define COUT( ANSWER ) cout << ( ANSWER ) << "\n" #define RETURN( ANSWER ) COUT( ANSWER ); QUIT #define SET_PRECISION( PRECISION ) cout << fixed << setprecision( PRECISION ) #define DOUBLE( PRECISION , ANSWER ) SET_PRECISION << ( ANSWER ) << "\n"; QUIT template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : -a; } template <typename T> inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : ( a % p ) + p; } #define POWER( ANSWER , ARGUMENT , EXPONENT ) \ static_assert( ! is_same<TYPE_OF( ARGUMENT ),int>::value && ! is_same<TYPE_OF( ARGUMENT ),uint>::value ); \ TYPE_OF( ARGUMENT ) ANSWER{ 1 }; \ { \ TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT ); \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #define POWER_MOD( ANSWER , ARGUMENT , EXPONENT , MODULO ) \ ll ANSWER{ 1 }; \ { \ ll ARGUMENT_FOR_SQUARE_FOR_POWER = ( MODULO + ( ( ARGUMENT ) % MODULO ) ) % MODULO; \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER = ( ANSWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % MODULO; \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT_FOR_SQUARE_FOR_POWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % MODULO; \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #define FACTORIAL_MOD( ANSWER , ANSWER_INV , INVERSE , MAX_INDEX , CONSTEXPR_LENGTH , MODULO ) \ static ll ANSWER[CONSTEXPR_LENGTH]; \ static ll ANSWER_INV[CONSTEXPR_LENGTH]; \ static ll INVERSE[CONSTEXPR_LENGTH]; \ { \ ll VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ ANSWER[0] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL; \ FOREQ( i , 1 , MAX_INDEX ){ \ ANSWER[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= i ) %= MODULO; \ } \ ANSWER_INV[0] = ANSWER_INV[1] = INVERSE[1] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ FOREQ( i , 2 , MAX_INDEX ){ \ ANSWER_INV[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= INVERSE[i] = MODULO - ( ( ( MODULO / i ) * INVERSE[MODULO % i] ) % MODULO ) ) %= MODULO; \ } \ } \ // 通常の二分探索その1 // EXPRESSIONがANSWERの狭義単調増加関数の時、EXPRESSION >= TARGETを満たす最小の整数を返す。 // 広義単調増加関数を扱いたい時は等号成立の処理を消して続く>に等号を付ける。 #define BS1( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \ ll ANSWER; \ { \ ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM; \ ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM; \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \ while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \ VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \ CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){ \ break; \ } else { \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH > 0 ){ \ VARIABLE_FOR_BINARY_SEARCH_U = ANSWER; \ } else { \ VARIABLE_FOR_BINARY_SEARCH_L = ANSWER + 1; \ } \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ } \ } \ CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << ">=0" ); \ } \ // 通常の二分探索その2 // EXPRESSIONがANSWERの狭義単調増加関数の時、EXPRESSION <= TARGETを満たす最大の整数を返す。 // 広義単調増加関数を扱いたい時は等号成立の処理を消して続く<に等号を付ける。 #define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \ ll ANSWER; \ { \ ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM; \ ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM; \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \ while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \ VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \ CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){ \ break; \ } else { \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH < 0 ){ \ VARIABLE_FOR_BINARY_SEARCH_L = ANSWER; \ } else { \ VARIABLE_FOR_BINARY_SEARCH_U = ANSWER - 1; \ } \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ } \ } \ CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "<=0" ); \ } \ // 通常の二分探索その3 // EXPRESSIONがANSWERの狭義単調減少関数の時、EXPRESSION >= TARGETを満たす最大の整数を返す。 // 広義単調増加関数を扱いたい時は等号成立の処理を消して続く>に等号を付ける。 #define BS3( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \ ll ANSWER; \ { \ ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM; \ ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM; \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \ while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \ VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \ CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){ \ break; \ } else { \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH > 0 ){ \ VARIABLE_FOR_BINARY_SEARCH_L = ANSWER; \ } else { \ VARIABLE_FOR_BINARY_SEARCH_U = ANSWER - 1; \ } \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ } \ } \ CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << ">=0" ); \ } \ // 通常の二分探索その4 // EXPRESSIONがANSWERの狭義単調減少関数の時、EXPRESSION <= TARGETを満たす最小の整数を返す。 // 広義単調増加関数を扱いたい時は等号成立の処理を消して続く<に等号を付ける。 #define BS4( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \ ll ANSWER; \ { \ ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM; \ ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM; \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \ while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \ VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \ CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){ \ break; \ } else { \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH < 0 ){ \ VARIABLE_FOR_BINARY_SEARCH_U = ANSWER; \ } else { \ VARIABLE_FOR_BINARY_SEARCH_L = ANSWER + 1; \ } \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ } \ } \ CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "<=0" ); \ } \ // 二進法の二分探索 // EXPRESSIONがANSWERの狭義単調増加関数の時、EXPRESSION <= TARGETを満たす最大の整数を返す。 #define BBS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ ll ANSWER = MINIMUM; \ { \ ll VARIABLE_FOR_POWER_FOR_BINARY_SEARCH = 1; \ ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( MAXIMUM ) - ANSWER; \ while( VARIABLE_FOR_POWER_FOR_BINARY_SEARCH <= VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ){ \ VARIABLE_FOR_POWER_FOR_BINARY_SEARCH *= 2; \ } \ VARIABLE_FOR_POWER_FOR_BINARY_SEARCH /= 2; \ ll VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH = ANSWER; \ while( VARIABLE_FOR_POWER_FOR_BINARY_SEARCH != 0 ){ \ ANSWER = VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH + VARIABLE_FOR_POWER_FOR_BINARY_SEARCH; \ VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){ \ VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH = ANSWER; \ break; \ } else if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH < 0 ){ \ VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH = ANSWER; \ } \ VARIABLE_FOR_POWER_FOR_BINARY_SEARCH /= 2; \ } \ ANSWER = VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH; \ } \ // 圧縮用 #define TE template #define TY typename #define US using #define ST static #define IN inline #define CL class #define PU public #define OP operator #define CE constexpr #define CO const #define NE noexcept #define RE return #define WH while #define VO void #define VE vector #define LI list #define BE begin #define EN end #define SZ size #define MO move #define TH this #define CRI CO int& #define CRUI CO uint& #define CRL CO ll& int LibrarySearch() { CERR( "ライブラリーを探索しますか?[y/n]" ); CIN( string , reply ); if( reply == "n" ){ CERR( "ライブラリーを探索せずに続行します。" ); CERR( "" ); return 0; } else if( reply != "y" ){ CERR( "y/nのいずれかで答えてください。" ); CERR( "終了します。" ); CERR( "" ); return -1; } CERR( "" ); CERR( "ライブラリーを探索します。" ); CERR( "問題の種類を番号で指定してください;" ); vector<string> problems = { "数に関する問題。" , "配列に関する問題。" , "文字列に関する問題。" , "順列に関する問題。" , "矩形領域に関する問題。" , "グラフに関する問題。" , "格子点に関する問題。" , "ナップサック問題。" }; int problems_size = problems.size(); FOR( i , 0 , problems_size ){ CERR( i << ": " << problems[i] ); } CIN( int , num ); CERR( "" ); if( num < 0 || num >= problems_size ){ cerr << "返答は" << problems_size - 1 << "以下の非負整数にしてください。"; CERR( "終了します。" ); CERR( "" ); return -1; } else if( num == 0 ){ CERR( "入力は1つの数か、1つの数と法を表す数ですか?[y/n/c]" ); cin >> reply; CERR( "" ); if( reply == "y" ){ CERR( "まずは小さい入力の場合を愚直に計算し、OEISで検索しましょう。" ); CERR( "https://oeis.org/?language=japanese" ); CERR( "" ); CERR( "次に出力の定義と等価な式を考察しましょう。" ); CERR( "- 単調ならば、冪乗や階乗を検討しましょう。" ); CERR( "- 定義にp進法が使われていれば、探索アルゴリズムを検討しましょう。" ); CERR( "- 入力が素数に近い場合に規則性があれば、p進付値、p進法、オイラー関数、約数の個数などを検討しましょう。" ); } else if( reply == "n" ){ CERR( "このケースのライブラリー探索は未実装です。" ); } else if( reply == "c" ){ CERR( "終了します。" ); CERR( "" ); return -1; } else { CERR( "y/n/cのいずれかで答えてください。" ); CERR( "終了します。" ); CERR( "" ); return -1; } CERR( "" ); CERR( "マルチテストケースの場合は以下の前計算を検討しましょう;" ); CERR( "素数列挙、約数列挙、サブゴールとなる関係式を満たす解列挙。" ); } else if( num == 3 ){ CERR( "符号の計算は転倒数の計算に帰着させましょう。" ); CERR( "符号と何かの積の和は行列式に帰着させましょう。余因子展開のメモ化再帰でO(N 2^N)です。" ); CERR( "" ); CERR( "1つの順列の転倒数は、" ); CERR( "- O(N^2)が通るなら愚直な二重ループ" ); CERR( "- O(N log_2 N)が必要なら可換群に対するBIT" ); CERR( " \\Mathematics\\Combinatorial\\Permutation" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\BIT" ); CERR( "で計算しましょう。" ); CERR( "" ); CERR( "条件を満たす順列全体をわたる転倒数の総和は、各i<jごとにそこで転倒が生じる順列の個数を計算し、その総和を取りましょう。" ); CERR( "条件が良ければ、転倒が生じる順列の個数は転倒が生じるとは限らない順列の個数の半分となります。" ); } else if( num == 4 ){ CERR( "入力に各座標の壁や移動コストが与えられる迷路の問題は、BFSやDijkstraを検討しましょう。" ); CERR( "\\Utility\\Search\\BreadthFirst" ); CERR( "\\Utility\\Search\\Dijkstra" ); CERR( "" ); CERR( "入力に到達すべき点が複数与えられる最短経路問題は、HeldKarpや移動方法を分類するパラメータの全探策などを検討しましょう。" ); CERR( "" ); CERR( "その他の最小/最大化問題は、" ); CERR( "- O(HW)が通るならば動的計画法" ); CERR( "- O(HW)が通らないならば見方を変えて上の問題に帰着できないか" ); CERR( "を検討しましょう。" ); CERR( "" ); CERR( "例えば迷路の攻略可能性は" ); CERR( "- スタートとゴールが同一の弧状連結成分に属すこと" ); CERR( "- スタートとゴールを分断する壁のパスの非存在性" ); CERR( "などから翻訳できます。" ); } else { CERR( "このケースのライブラリー探索は未実装です。" ); } CERR( "" ); CERR( "終了します。" ); CERR( "" ); return -1; } #define DIJKSTRA_BODY( INITIALISE_PREV , SET_PREV ) \ static const U& unit = Unit(); \ assert( unit != m_found && unit < m_infty ); \ U weight[size_max]; \ \ for( int i = 0 ; i < m_size ; i++ ){ \ \ weight[i] = m_infty; \ \ } \ \ set<pair<U,int> > vertex{}; \ const int i_start = e_inv( t_start ); \ const int i_final = e_inv( t_final ); \ vertex.insert( pair<U,int>( weight[i_start] = unit , i_start ) ); \ INITIALISE_PREV; \ \ while( ! vertex.empty() ){ \ \ auto itr_vertex = vertex.begin(); \ const pair<U,int> v = *itr_vertex; \ const int& i = v.second; \ \ if( i == i_final ){ \ \ break; \ \ } \ \ const U& u = v.first; \ weight[i] = m_found; \ vertex.erase( itr_vertex ); \ const list<pair<T,U> > edge_i = E( e( i ) ); \ list<pair<U,int> > changed_vertex{}; \ \ for( auto itr_edge_i = edge_i.begin() , end_edge_i = edge_i.end() ; itr_edge_i != end_edge_i ; itr_edge_i++ ){ \ \ const int& j = e_inv( itr_edge_i->first ); \ U& weight_j = weight[j]; \ \ if( weight_j != m_found ){ \ \ const U& edge_ij = itr_edge_i->second; \ const U temp = Add( u , edge_ij ); \ assert( edge_ij != m_found && temp != m_found && !( temp < edge_ij ) && temp < m_infty ); \ \ if( weight_j > temp ){ \ \ if( weight_j != m_infty ){ \ \ vertex.erase( pair<U,int>( weight_j , j ) ); \ \ } \ \ SET_PREV; \ changed_vertex.push_back( pair<U,int>( weight_j = temp , j ) ); \ \ } \ \ } \ \ } \ \ for( auto itr_changed = changed_vertex.begin() , end_changed = changed_vertex.end() ; itr_changed != end_changed ; itr_changed++ ){ \ \ vertex.insert( *itr_changed ); \ \ } \ \ } \ template <typename T , typename U , list<pair<T,U> > E(const T&) , int size_max> class DijkstraBody { private: int m_size; U m_infty; U m_found; int m_length; map<T,int> m_memory; vector<T> m_memory_inv; public: inline DijkstraBody( const int& size , const U& infty , const U& found ); U Solve( const T& t_start , const T& t_final ); U Solve( const T& t_start , const T& t_final , list<T>& path ); private: virtual const U& Unit() const = 0; virtual U Add( const U& , const U& ) const = 0; virtual T e( const int& i ); virtual int e_inv( const T& t ); virtual void Reset(); }; // 入力の範囲内で要件 // (1) Eの値の各成分の第2成分が0以上である。 // (2) 2^{31}-1がEの値の各成分の第2成分size_max個以下の和で表せるいかなる数よりも大きい。 // が成り立つ場合にのみサポート。 template <list<pair<int,ll> > E(const int&) , int size_max> class Dijkstra : public DijkstraBody<int,ll,E,size_max> { public: inline Dijkstra( const int& size ); private: inline const ll& Unit() const; inline ll Add( const ll& , const ll& ) const; inline int e( const int& i ); inline int e_inv( const int& t ); inline void Reset(); }; // 入力の範囲内で要件 // (1) Eの値の各成分の第2成分がe_T()以上である。 // (2) inftyがEの値の各成分の第2成分size_max個以下の和で表せるいかなる項よりも大きい。 // (3) foundがEの値の各成分の第2成分size_max個以下の和で表せず、inftyとも異なる。 // (4) (U,m_U:U^2->U,e_U:1->U)がbool operator<(const U&,const U&)に関して順序モノイドである。 // が成り立つ場合にのみサポート。 template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max> class MemorisationDijkstra : public DijkstraBody<T,U,E,size_max> { public: inline MemorisationDijkstra( const int& size , const U& infty = 2147483647 , const U& found = -1 ); private: inline const U& Unit() const; inline U Add( const U& , const U& ) const; }; // 入力の範囲内で要件 // (1) Eの値の各成分の第2成分がe_T()以上である。 // (2) inftyがEの値の各成分の第2成分size_max個以下の和で表せるいかなる項よりも大きい。 // (3) foundがEの値の各成分の第2成分size_max個以下の和で表せず、inftyとも異なる。 // (4) (U,m_U:U^2->U,e_U:1->U)がbool operator<(const U&,const U&)に関して順序モノイドである。 // (5) (enum_T,enum_T_inv)が互いに逆写像である。 // が成り立つ場合にのみサポート。 template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max , T enum_T(const int&) , int enum_T_inv(const T&)> class EnumerationDijkstra : public DijkstraBody<T,U,E,size_max> { public: inline EnumerationDijkstra( const int& size , const U& infty = 2147483647 , const U& found = -1 ); private: inline const U& Unit() const; inline U Add( const U& , const U& ) const; inline T e( const int& i ); inline int e_inv( const T& t ); inline void Reset(); }; template <typename T , typename U , list<pair<T,U> > E(const T&) , int size_max> inline DijkstraBody<T,U,E,size_max>::DijkstraBody( const int& size , const U& infty , const U& found ) : m_size( size ) , m_infty( infty ) , m_found( found ) , m_length() , m_memory() , m_memory_inv() {} template <list<pair<int,ll> > E(const int&) , int size_max> inline Dijkstra<E,size_max>::Dijkstra( const int& size ) : DijkstraBody<int,ll,E,size_max>( size , 2147483647 , -1 ) {} template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max> inline MemorisationDijkstra<T,U,m_U,e_U,E,size_max>::MemorisationDijkstra( const int& size , const U& infty , const U& found ) : DijkstraBody<T,U,E,size_max>( size , infty , found ) {} template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline EnumerationDijkstra<T,U,m_U,e_U,E,size_max,enum_T,enum_T_inv>::EnumerationDijkstra( const int& size , const U& infty , const U& found ) : DijkstraBody<T,U,E,size_max>( size , infty , found ) {} template <typename T , typename U , list<pair<T,U> > E(const T&) , int size_max> U DijkstraBody<T,U,E,size_max>::Solve( const T& t_start , const T& t_final ) { DIJKSTRA_BODY( , ); Reset(); return weight[i_final]; } template <typename T , typename U , list<pair<T,U> > E(const T&) , int size_max> U DijkstraBody<T,U,E,size_max>::Solve( const T& t_start , const T& t_final , list<T>& path ) { DIJKSTRA_BODY( T prev[size_max] = {} , prev[j] = i ); int i = i_final; while( i != i_start ){ path.push_front( e( i ) ); i = prev[i]; } path.push_front( t_start ); Reset(); return weight[i_final]; } template <list<pair<int,ll> > E(const int&) , int size_max> inline const ll& Dijkstra<E,size_max>::Unit() const { static const ll unit = 0; return unit; } template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max> inline const U& MemorisationDijkstra<T,U,m_U,e_U,E,size_max>::Unit() const { return e_U(); } template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline const U& EnumerationDijkstra<T,U,m_U,e_U,E,size_max,enum_T,enum_T_inv>::Unit() const { return e_U(); } template <list<pair<int,ll> > E(const int&) , int size_max> inline ll Dijkstra<E,size_max>::Add( const ll& u0 , const ll& u1 ) const { return u0 + u1; } template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max> inline U MemorisationDijkstra<T,U,m_U,e_U,E,size_max>::Add( const U& u0 , const U& u1 ) const { return m_U( u0 , u1 ); } template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline U EnumerationDijkstra<T,U,m_U,e_U,E,size_max,enum_T,enum_T_inv>::Add( const U& u0 , const U& u1 ) const { return m_U( u0 , u1 ); } template <typename T , typename U , list<pair<T,U> > E(const T&) , int size_max> T DijkstraBody<T,U,E,size_max>::e( const int& i ) { assert( i < m_length ); return m_memory_inv[i]; } template <list<pair<int,ll> > E(const int&) , int size_max> inline int Dijkstra<E,size_max>::e( const int& i ) { return i; } template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline T EnumerationDijkstra<T,U,m_U,e_U,E,size_max,enum_T,enum_T_inv>::e( const int& i ) { return enum_T( i ); } template <typename T , typename U , list<pair<T,U> > E(const T&) , int size_max> int DijkstraBody<T,U,E,size_max>::e_inv( const T& t ) { if( m_memory.count( t ) == 0 ){ assert( m_length < m_size ); m_memory_inv.push_back( t ); return m_memory[t] = m_length++; } return m_memory[t]; } template <list<pair<int,ll> > E(const int&) , int size_max> inline int Dijkstra<E,size_max>::e_inv( const int& t ) { return t; } template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline int EnumerationDijkstra<T,U,m_U,e_U,E,size_max,enum_T,enum_T_inv>::e_inv( const T& t ) { return enum_T_inv( t ); } template <typename T , typename U , list<pair<T,U> > E(const T&) , int size_max> void DijkstraBody<T,U,E,size_max>::Reset() { m_length = 0; m_memory.clear(); m_memory_inv.clear(); return; } template <list<pair<int,ll> > E(const int&) , int size_max> inline void Dijkstra<E,size_max>::Reset() {} template <typename T , typename U , U m_U(const U&,const U&) , const U& e_U() , list<pair<T,U> > E(const T&) , int size_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline void EnumerationDijkstra<T,U,m_U,e_U,E,size_max,enum_T,enum_T_inv>::Reset() {} #ifdef DEBUG inline CEXPR( int , bound_H_W , 5 ); #else inline CEXPR( int , bound_H_W , 800 ); #endif int H , W , H_minus , W_minus; int A[bound_H_W][bound_H_W]; list<pair<pair<int,int>,ll> > E( const pair<int,int>& k ) { list<pair<pair<int,int>,ll> > edge{}; if( k.first < H ){ FOREQ( di , -1 , 1 ){ FOREQ( dj , -1 , 1 ){ int i_next = k.first + di; int j_next = k.second + dj; if( 1 <= i_next && i_next < H_minus && 0 <= j_next && j_next < W && ( i_next != k.first || j_next != k.second ) ){ int& Aij_next = A[i_next][j_next]; if( Aij_next != -1 ){ edge.push_back( { { i_next , j_next } , Aij_next } ); } } } } if( k.second == W_minus ){ edge.push_back( pair<pair<int,int>,ll>( { H , 1 } , 0 ) ); } } else if( k.first == H && k.second == 0 ){ FOR( i , 1 , H_minus ){ int& Ai0 = A[i][0]; if( Ai0 != -1 ){ edge.push_back( { { i , 0 } , A[i][0] } ); } } } return edge; } inline ll Add( const ll& u0 , const ll& u1 ) { return u0 + u1; } inline const ll& Zero() { static const ll zero = 0; return zero; } int main() { UNTIE; LIBRARY_SEARCH; SET_ASSERT( H , 3 , bound_H_W ); SET_ASSERT( W , 3 , bound_H_W ); H_minus = H - 1; W_minus = W - 1; CEXPR( int , bound_Aij , 800 ); FOR( i , 1 , H_minus ){ int ( &Ai )[bound_H_W] = A[i]; FOR( j , 0 , W ){ CIN_ASSERT( Aij , -1 , bound_Aij ); assert( Aij != 0 ); Ai[j] = Aij; } } CEXPR( int , infty , bound_H_W * bound_H_W * bound_Aij ); MemorisationDijkstra<pair<int,int>,ll,Add,Zero,E,bound_H_W * bound_H_W + 2> d{ H * W + 2 }; ll answer = d.Solve( { H , 0 } , { H , 1 } ); RETURN( answer < infty ? answer : -1 ); }