bzoj4145:[AMPPZ2014]The Prices

本文介绍了一种使用状态压缩动态规划(状压DP)解决特定路径问题的方法,通过记录前缀最小值来优化计算过程,适用于需要考虑是否访问某些节点的场景,如在商店路径问题中决定是否进入某家店铺。

传送门

状压dp
考虑某些店可以不走,所以做的时候记个前缀min就好了
代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
void read(int &x) {
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e5+10;
int n,m,d[105],f[105][1<<16],c[105][16],ans=2e9;
int main()
{
    read(n),read(m);
    for(rg int i=1;i<=n;i++)
    {
        read(d[i]);
        for(rg int j=0;j<m;j++)read(c[i][j]);
    }
    int tot=1<<m;memset(f,63,sizeof f);
    f[0][0]=0;
    for(rg int i=1;i<=n;i++)
    {
        for(rg int j=0;j<tot;j++)
        {
            f[i][j]=f[i-1][j]+d[i];
            for(rg int k=0;k<m;k++)
                if(j&(1<<k))f[i][j]=min(f[i][j],f[i][j^(1<<k)]+c[i][k]);
        }
        for(int j=0;j<tot;j++)f[i][j]=min(f[i][j],f[i-1][j]);
    }
    printf("%d\n",f[n][tot-1]);
}

转载于:https://www.cnblogs.com/lcxer/p/10512015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值