bzoj4145 [AMPPZ2014]価格状压DP

トピックポータル

https://lydsy.com/JudgeOnline/problem.php?id=4145

問題の解決策

この質問はそれを行うための方法がたくさんあるようです。

少しシンプル、マルチ聖歌をしたのはなぜこの質問は...あります。

私の方法:

取得する([S] F \)\は、同じ店購入することを約束した\(S \)商品の最小コストを。

次に\(DP [S] \)後表す\(S \)商品の最小コスト。列挙のサブセットに転送します。

時間複雑\(O(M2 ^ ^ n型のn + 3)\)


良いアイデアもあります。

\(DP [I] [S ] \) 前回表し\(私は\)店が買う(S \)\記事の最小コストを。

あなたが決めるのであればと、転送は、あなたがしたい(私は\)\すべての状態の後、最初のプラスの費用、およびそれに転送された各商品のバックパックを列挙-で購入します。


ちょうど私のアプローチを記述します。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

#define lowbit(x) ((x) & -(x))

const int N = 16 + 7;
const int M = 100 + 7;
const int NP = (1 << 16) + 7;

int m, n, S;
int a[M][N];
int f[NP], t[NP], dp[NP];

inline void ycl() {
    S = (1 << n) - 1;
    memset(f, 0x3f, sizeof(f));
    for (int i = 1; i <= m; ++i) {
        t[0] = a[i][0];
        for (int s = 1; s <= S; ++s) t[s] = t[s ^ lowbit(s)] + a[i][std::__lg(lowbit(s)) + 1];
        for (int s = 0; s <= S; ++s) smin(f[s], t[s]);
    }
}

inline void work() {
    ycl();
    memset(dp, 0x3f, sizeof(dp));
    dp[0] = 0;
    for (int s = 1; s <= S; ++s)
        for (int sta = s; sta; sta = (sta - 1) & s)
            smin(dp[s], f[sta] + dp[s ^ sta]);
    printf("%d\n", dp[S]);
}

inline void init() {
    read(m), read(n);
    for (int i = 1; i <= m; ++i)
        for (int j = 0; j <= n; ++j) read(a[i][j]);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

おすすめ

転載: www.cnblogs.com/hankeke/p/bzoj4145.html