P1507 NASA的食物計劃(二維費用揹包)

題目傳送門

題意: 給你一個揹包,揹包容積爲v,承重量爲m,你有一些物品,他們有各自的體積、質量和價值,在這個揹包體積和承重量範圍內,裝入最大總價值的物品,求最大的價值。

思路: 二維費用揹包的板子題,看做一維01揹包鑲嵌一個一維01揹包。

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e5+5;
const int inf=0x3f3f3f3f;
const int mod1=1e9+7;
const int mod2=993471257;
const double eps=1e-7;
const double PI=acos(-1);
int vi[N],mi[N],ka[N],f[500][500];
signed main()
{
    int v,m;
    cin>>v>>m;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>vi[i]>>mi[i]>>ka[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=v;j>=vi[i];j--)
        {
            for(int k=m;k>=mi[i];k--)
            {
                f[j][k]=max(f[j][k],f[j-vi[i]][k-mi[i]]+ka[i]);
            }
        }
    }
    cout<<f[v][m]<<endl;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章