bzoj4145 [AMPPZ2014]The Prices 状压 DP

Topic Portal

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

answer

This question seems to have a lot of way to do that.

This question is ... Why did a little simple, multi chant.

My method:

Obtain \ (f [S] \) pledged to buy the same store \ (S \) minimum cost of goods.

Then \ (dp [S] \) represents a later \ (S \) minimum cost of goods. Transfer to a subset of enumeration.

Time complexity \ (O (M2 ^ ^ n-n-+. 3) \) .


There is also a good idea:

\ (dp [i] [S ] \) represents the previous \ (I \) stores buy (S \) \ minimum cost of the article.

So when the transfer, if you decide you want to \ (i \) buy in - then the first plus expenses for all the states, and then enumerate each commodity backpack transferred on it.


Just write my approach.

#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;
}

Guess you like

Origin www.cnblogs.com/hankeke/p/bzoj4145.html