码迷,mamicode.com
首页 > 其他好文 > 详细

Bzoj 4145: [AMPPZ2014]The Prices

时间:2019-01-13 17:10:37      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:cond   clu   put   mpp   max   getch   pair   stream   mat   

Bzoj 4145: [AMPPZ2014]The Prices

状态压缩dp

\(f[i][j]\)表示前i个商店 , 状态为j的最小花费.

考虑什么东西也不买和买了东西.

买了东西的话,就要到i地.

然后转移:\(f[i][j] = min(f[i][j] , f[i][j ^ (1 << k - 1)] + c[i][k])\)

不买东西

\(f[i][j] = f[i - 1][j]\)

/*header*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#define rep(i , x, p) for(int i = x;i <= p;++ i)
#define sep(i , x, p) for(int i = x;i >= p;-- i)
#define gc getchar()
#define pc putchar
#define ll long long
#define mk make_pair
#define fi first
#define se second
using std::min;
using std::max;
using std::swap;

inline int gi() {
    int x = 0,f = 1;char c = gc;
    while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}return x * f;
}

void print(int x) {
    if(x < 0) pc('-') , x = -x;
    if(x >= 10) print(x / 10);
    pc(x % 10 + '0');
}

const int maxN = 100 + 7;

void gmin(int &x , int y) {
    x = x > y ? y : x;
}

int f[maxN][65540] , c[maxN][maxN], d[maxN];

int main() {
    int n = gi() , m = gi();
    rep(i , 1, n) {
        d[i] = gi();
        rep(j , 1, m) c[i][j] = gi();
    }
    memset(f , 0x3f, sizeof(f));
    f[0][0] = 0;
    rep(i , 1, n) {
        rep(j , 0, (1 << m) - 1) f[i][j] = f[i - 1][j] + d[i];
        rep(j , 0, (1 << m) - 1) {
            rep(k , 1, m) {
                if(j & (1 << k - 1))
                    f[i][j] = min(f[i][j] , f[i][j ^ (1 << k - 1)] + c[i][k]);
            }
        }
        rep(j , 0, (1 << m) - 1) f[i][j] = min(f[i][j] , f[i - 1][j]);
    }
    printf("%d\n", f[n][(1 << m) - 1]);
    return 0;
}

Bzoj 4145: [AMPPZ2014]The Prices

标签:cond   clu   put   mpp   max   getch   pair   stream   mat   

原文地址:https://www.cnblogs.com/gzygzy/p/10263060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!