zoukankan      html  css  js  c++  java
  • BZOJ 4145: [AMPPZ2014]The Prices( 状压dp + 01背包 )

    我自己只能想出O( n*3^m )的做法....肯定会T

    O( nm*2^m )做法:

    dp( x, s ) 表示考虑了前 x 个商店, 已买的东西的集合为s.

    考虑转移 : 先假设我们到第x个商店去, so初始时 dp( x, s) = dp( x-1, s ) + d[x]

    然后我们可以对第x个商店做01背包, dp(x, s + {h} ) = min( dp( x, s + {h} ) , dp( x, s) + c[x][h]) ) ( h ∉ s ).

    之后我们再比较到第x个商店划不划算 : dp(x, s) = min(dp(x - 1, s) , dp(x, s) ) 

    answer = dp(m, {1, 2, …… n } ) 

    ---------------------------------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
      
    #define rep(i, n) for(int i = 0; i < n; i++)
    #define clr(x, c) memset(x, c, sizeof(x))
    #define b(i) (1 <<(i))
      
    using namespace std;
     
    const int maxn = 105, maxm = 17, oo = int(1e9);
     
    int d, c[maxm], dp[2][b(maxm)], A = 0, B = 1;
     
    inline int read() {
    char c = getchar();
    for(; !isdigit(c); c = getchar());
    int ans = 0;
    for(; isdigit(c); c = getchar())
       ans = ans * 10 + c - '0';
    return ans;
    }
     
    int main() {
    freopen("test.in", "r", stdin);
    int n = read(), m = read(), all = b(m);
    rep(s, all) dp[A][s] = oo;
    dp[A][0] = 0;
    rep(i, n) {
    swap(A, B);
    d = read();
    rep(j, m) c[j] = read();
    rep(s, all) dp[A][s] = dp[B][s] + d;
    rep(j, m) 
       rep(s, all) if(!(s & b(j))) 
       dp[A][s | b(j)] = min(dp[A][s | b(j)], dp[A][s] + c[j]);
    rep(s, all) dp[A][s] = min(dp[A][s], dp[B][s]);
    }
    printf("%d ", dp[A][all - 1]);
    return 0;
    }

    --------------------------------------------------------------------------------- 

    4145: [AMPPZ2014]The Prices

    Time Limit: 20 Sec  Memory Limit: 256 MB
    Submit: 156  Solved: 99
    [Submit][Status][Discuss]

    Description

    你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i][j],
    求最小总费用。

    Input

    第一行包含两个正整数n,m(1<=n<=100,1<=m<=16),表示商店数和物品数。
    接下来n行,每行第一个正整数d[i](1<=d[i]<=1000000)表示到第i家商店的路费,接下来m个正整数,
    依次表示c[i][j](1<=c[i][j]<=1000000)。

    Output

    一个正整数,即最小总费用。

    Sample Input

    3 4
    5 7 3 7 9
    2 1 20 3 2
    8 1 20 1 1

    Sample Output

    16

    HINT

    在第一家店买2号物品,在第二家店买剩下的物品。


    Source

  • 相关阅读:
    The library 'hostpolicy.dll' required to execute the application was not found in
    矩阵乘法
    2019-11-1
    四边形不等式的应用
    2019-10-30
    2019-10-29
    差分与前缀和
    平衡树SPLAY
    可爱的树链剖分(染色)
    cable tv network
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4658426.html
Copyright © 2011-2022 走看看