01背包0分求助

CF19B Checkout Assistant

a2lyaXNhbWUgbWFyaXNh @ 2022-08-24 14:12:39

RT

根据题解里面的作法写的

加了初始化也没用QAQ(虽然没有爬过来,但我登上自己的CF号看的)

求调:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
int w[2050],c[2050],dp[114514];
int ans=2e12;
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>w[i]>>c[i];
        w[i]++;
        m=max(m,w[i]);
    }
    m+=n;
    for(int i=1;i<=n;i++)
        for(int j=m;j>=w[i];j--)
            dp[j]=min(dp[j],dp[j-w[i]]+c[i]);
    for(int i=1;i<=m;i++)
        ans=min(ans,dp[i]);
    cout<<ans;
    return 0;
}

by a2lyaXNhbWUgbWFyaXNh @ 2022-08-24 14:45:43

@AKNOI的梓钦 az(维生素片)


by xrxtcl @ 2022-08-24 14:45:56

还有,dp[0]要=0吧


by xrxtcl @ 2022-08-24 14:47:04

https://www.luogu.com.cn/record/84973029


by xrxtcl @ 2022-08-24 14:47:19


#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
int w[2050],c[2050],dp[114514];
int ans=2e12;
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>w[i]>>c[i];
        w[i]++;
        m=max(m,w[i]);
    }
    m+=n;
    memset(dp,0x7f,sizeof dp),dp[0]=0;
    for(int i=1;i<=n;i++)
        for(int j=m;j>=w[i];j--)
            dp[j]=min(dp[j],dp[j-w[i]]+c[i]);
    for(int i=n;i<=m;i++)
        ans=min(ans,dp[i]);
    cout<<ans;
    return 0;
}

by xrxtcl @ 2022-08-24 14:47:34

@S__B


by a2lyaXNhbWUgbWFyaXNh @ 2022-08-24 14:47:59

我是煞笔此贴spfa


by xrxtcl @ 2022-08-24 14:48:55

@S__B 感谢您帮我A掉一道蓝题thx


上一页 |