zoukankan      html  css  js  c++  java
  • bzoj4145 [AMPPZ2014]The Prices 状压 DP

    题目传送门

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

    题解

    好像这道题有不少方法呢。

    ...谁叫这道题有点简单,所以方法多呗。

    我的方法:

    求出 (f[S]) 表示要在同一家商店购买 (S) 中的物品的最小代价。

    然后 (dp[S]) 表示购买 (S) 中的商品的最小代价。枚举子集转移即可。

    时间复杂度 (O(m2^n+3^n))


    还有一个不错的做法:

    (dp[i][S]) 表示在前 (i) 个商店买 (S) 中的物品的最小代价。

    于是转移的时候,如果决定要在 (i) 中买——那么先对所有状态加上路费,然后枚举每一个商品背包转移就可以了。


    只写了我的做法。

    #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
    ", 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;
    }
    
  • 相关阅读:
    Linux命令之find exec rm-删除所有日志文件
    Linux常用系统信息查询命令
    shell后台运行脚本
    Linux 下后台运行程序
    Ubuntu配置和修改IP地址
    ssh免密码登录配置方法
    (转)linux如何将新硬盘挂载到home目录下
    Shell中if [ ] 中-a到-z的意思
    安防监控相关
    本地yum源的建立
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj4145.html
Copyright © 2011-2022 走看看