結果

問題 No.1284 Flip Game
ユーザー KudeKude
提出日時 2020-11-07 00:02:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 2,982 bytes
コンパイル時間 2,322 ms
コンパイル使用メモリ 218,628 KB
実行使用メモリ 24,344 KB
最終ジャッジ日時 2023-09-29 19:47:14
合計ジャッジ時間 4,829 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 9 ms
4,868 KB
testcase_16 AC 9 ms
4,868 KB
testcase_17 AC 26 ms
9,292 KB
testcase_18 AC 24 ms
9,076 KB
testcase_19 AC 104 ms
23,888 KB
testcase_20 AC 31 ms
9,372 KB
testcase_21 AC 32 ms
9,224 KB
testcase_22 AC 30 ms
9,128 KB
testcase_23 AC 137 ms
24,344 KB
testcase_24 AC 138 ms
24,340 KB
testcase_25 AC 139 ms
24,208 KB
testcase_26 AC 107 ms
23,824 KB
testcase_27 AC 111 ms
23,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

template<class T>
std::vector<unsigned long long> dijkstra(std::vector<std::vector<std::pair<int,T>>>& to, int s=0) {
    const unsigned long long INF = 1002003004005006007;
    struct QueElem {
        int v;
        unsigned long long c;
        bool operator<(const QueElem a) const {return c > a.c;}
        QueElem(int v, unsigned long long c): v(v), c(c) {}
    };
    std::priority_queue<QueElem> q;
    std::vector<unsigned long long> dist(to.size(), INF);
    dist[s] = 0;
    q.emplace(s, 0);
    while(!q.empty()) {
        QueElem qe = q.top(); q.pop();
        int u = qe.v;
        unsigned long long c = qe.c;
        if (c > dist[u]) continue;
        for(auto vc: to[u]) {
            int v = vc.first;
            unsigned long long nc = c + vc.second;
            if (nc < dist[v]) {
                dist[v] = nc;
                q.emplace(v, nc);
            }
        }
    }
    return dist;
}

int n;
pair<VI,int> decode(int x) {
    pair<VI,int> ret;
    VI& s = ret.first;
    int& u = ret.second;
    s.resize(n);
    int t = 1;
    rep(_, n) t *= 3;
    u = x / t;
    x %= t;
    rep(i, n) {
        s[i] = x % 3;
        x /= 3;
    }
    return ret;
}

int encode(VI s, int u) {
    int ret = 0;
    int t = 1;
    rep(i, n) {
        ret += t * s[i];
        t *= 3;
    }
    ret += t * u;
    return ret;
}

VVI c;
vector<vector<pair<int,ll>>> to;
void dfs(VI& s, int i=0) {
    if (i == n) {
        rep(orig, n) {
            if (s[orig] == 0) continue;
            int oi = encode(s, orig);
            rep(dest, n) {
                if (orig == dest) continue;
                if (s[dest] == 2) continue;
                s[dest]++;
                int di = encode(s, dest);
                to[oi].emplace_back(di, c[orig][dest]);
                s[dest]--;
            }
        }
        return;
    }
    rep(k, 3) {
        s[i] = k;
        dfs(s, i + 1);
    }
}

int pow3(int x) {
    int t = 1;
    rep(_, x) t *= 3;
    return t;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    c = VVI(n, VI(n));
    rep(i, n) rep(j, n) cin >> c[i][j];
    int t = 1;
    rep(i, n) t *= 3;
    to.resize(t * n);
    vector<int> s(n);
    dfs(s);
    for(int i = 0; i < n; ++i) {
        int dest = pow3(i) + t * i;
        to[0].emplace_back(dest, 0);
    }
    auto res = dijkstra(to);
    int a = t - 1;
    unsigned long long ans = 1002003004005006007;
    rep(u, n) {
        chmin(ans, res[t * u + a]);
        chmin(ans, res[t * u + a - pow3(u)]);
    }
    cout << ans << endl;
}
0