1 条题解

  • 0
    @ 2026-4-5 20:13:50

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int n, L, c[509], l[509];
    int f[2009]; //f[j]:买总容量不低于L的饮料的最小花费
    
    int main()
    {
        cin >> n >> L; // n种饮料   总容量不低于L
        for (int i = 1; i <= n; i++)
            cin >> c[i] >> l[i]; // 每一种饮料的售价和容量
        
        memset(f , 0x3f , sizeof(f));
        f[0] = 0;
    
        for(int i = 1; i <= n; i++)
        {
            for(int j = L; j >= 0; j--)
                f[j] = min(f[j] , f[max(0 , j - l[i])] + c[i]);
        }
        
        if(f[L] != 0x3f3f3f3f)
            cout << f[L];
        else
            cout << "no solution";
        return 0;
    }
    
    
    • 1

    信息

    ID
    713
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者