这道题用记忆化搜素可以过吗?求大佬讲解

P1064 [NOIP2006 提高组] 金明的预算方案

FASTergood @ 2024-04-16 18:50:33

不是题解请勿copy

```cpp

#include<iostream>
#include<algorithm>
using namespace std;
int g[105][105], a[1000], v[1000], h[1000];

int m, n, i, j,ans=0,k=0,vec[1000],sum=0;
int dfs(int w,int x)
{
    if (h[x] == 0)vec[x] = 2;
    else --vec[h[x]];
    if (w > m)return 0;
    if (vec[x] < 0)return 0;
    if (g[w][x])return g[w][x];
    if (w <= m) w+=v[x],ans+=v[x]*a[x];
    for (int i = 1;i <= n - x;i++)
    {
         dfs(w, x + i);
    }
        g[w][x] = ans;
        return ans;
}
int main()
{
    memset(h, -1, sizeof(h));

    cin >> m >> n;
    for (int i = 1;i <= n;i++)
    {
        cin >> a[i] >> v[i] >> h[i];
    }
    cout << dfs(a[1], 1);

}

|