結果
| 問題 | No.1284 Flip Game | 
| コンテスト | |
| ユーザー |  milanis48663220 | 
| 提出日時 | 2020-11-06 22:53:48 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 126 ms / 2,000 ms | 
| コード長 | 1,957 bytes | 
| コンパイル時間 | 1,579 ms | 
| コンパイル使用メモリ | 84,168 KB | 
| 実行使用メモリ | 21,888 KB | 
| 最終ジャッジ日時 | 2024-07-22 13:28:19 | 
| 合計ジャッジ時間 | 2,395 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 28 | 
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
int n;
ll c[10][10];
const ll INF = 1e18;
ll dp[1<<18][9];
void input(){
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> c[i][j];
        }
    }
}
void init_dp(){
    for(int i = 0; i < (1<<(2*n)); i++){
        for(int j = 0; j < n; j++){
            dp[i][j] = INF;
        }
    }
}
void solve(){
    init_dp();
    for(int i = 0; i < n; i++){
        dp[1<<i][i] = 0;
    }
    // l: Bのひっくり返し状況
    for(int l = 0; l < (1<<n); l++){
        // r: 盤面の状態
        for(int r = 0; r < (1<<n); r++){
            int m = (l<<n)+r;
            for(int p = 0; p < n; p++){
                for(int q = 0; q < n; q++){
                    if((1<<q)&r) continue;
                    if(!((1<<p)&r)) continue;
                    // cout << dp[m][p] << '-';
                    int next_m = 0;
                    // すでにpをひっくり返している
                    if(l&(1<<p)){
                        next_m = (l<<n)+(r+(1<<q));
                    // まだ
                    }else{
                        next_m = ((l+(1<<p))<<n)+(r+(1<<q)-(1<<p));
                    }
                    // cout << dp[next_m][q] << '-';
                    dp[next_m][q] = min(dp[next_m][q], dp[m][p]+c[p][q]);
                    // cout << l << ' ' << r << ' ' << p << ' ' << q << ' ' << dp[next_m][q] << endl;
                }
            }
        }
    }
    ll ans = INF;
    int full_bit = (1<<n)-1;
    for(int l = 0; l < (1<<n); l++){
        for(int p = 0; p < n; p++){
            ans = min(ans, dp[(l<<n)+full_bit][p]);
        }
    }
    cout << ans << endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    input();
    solve();
}
            
            
            
        