【bzoj4145】[AMPPZ2014]The Prices

本文深入探讨了状压背包算法的概念、原理及其优化策略。通过实例代码演示了如何解决具有状态压缩特性的背包问题,包括算法的时间复杂度分析与优化技巧。详细解释了状压背包在实际应用中的优势及局限性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

状压背包
不太好说
很容易写

然而一开始写了个O(n3m)的T傻了没反应过来……

总之这个就是当前走到了第i个商店,购买状态是s,然后背包转移,O(nm2n)

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define For(i,a,b) for(int i=a;i< b;i++)
#define fors(s0,s) for(int s0=s;s0;s0=(s0-1)&s)
#define shl(i) (1 << (i))

inline int rd() {
    char c = getchar();
    while (!isdigit(c)) c = getchar() ; int x = c - '0';
    while (isdigit(c = getchar())) x = x * 10 + c - '0';
    return x;
}

inline void upmin(int&a , int b) { if (a > b) a = b ; }

int n , m , d[101] , c[101][16] , all , f[2][65536];

void input() {
    n = rd() , m = rd();
    For(i , 0 , n) {
        d[i] = rd();
        For(j , 0 , m) c[i][j] = rd();
    }
    all = shl(m);
}

void solve() {
    int now = 0 , pre = 1;
    memset(f , 0x3f , sizeof f);
    f[0][0] = 0;
    For(i , 0 , n) {
        now ^= 1 , pre ^= 1;
        For(s , 0 , all) f[now][s] = f[pre][s] + d[i];
        For(k , 0 , m) For(s , 0 , all) if (!(s & shl(k)))
            upmin(f[now][s | shl(k)] , f[now][s] + c[i][k]);
        For(s , 0 , all) upmin(f[now][s] , f[pre][s]);
    }
    printf("%d\n" , f[now][all - 1]);
}

int main() {
    input();
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值