蓝桥杯Java ABC组 LG P1336 最佳课题选择

题目链接:
https://www.luogu.com.cn/problem/P1336

#背包问题

一道经典的泛化物品的背包问题

本题的泛化物品:每个课题是一个物品,对于每个课题物品,当体积为 x x x 时,价值为 A i × x B i A_i\times x^{B_i} Ai×xBi

而本题题意所求的就是:体积恰好为 n 的情况下,价值的最小值

注意“恰好”和“最小值”的限制

[!danger]
十年 OI 一场空,不开 __ __ 见祖宗

代码

import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Main{

    static int status;
    static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static PrintWriter cout = new PrintWriter(bw);
    static StreamTokenizer st = new StreamTokenizer(buf);

    public static int nextInt() throws IOException {
        status = st.nextToken();
        return (int) st.nval;
    }

    public static long nextLong() throws IOException {
        status = st.nextToken();
        return (long) st.nval;
    }

    public static String nextString() throws IOException {
        status = st.nextToken();
        return st.sval;
    }

	static final int INF = 0x3f3f3f3f, MOD = (int) 1e9 + 7;
    static int n, m;
    static int[] a,b;
    static long[] f;
    
    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        a = new int[m+1];
        b = new int[m+1];
        f = new long[n+1];
        Arrays.fill(f,Long.MAX_VALUE/2);

        for(int i=1;i<=m;i++){
            a[i] = nextInt();
            b[i] = nextInt();
        }
        f[0]=0;
        for(int i=1;i<=m;i++)
            for(int j=n;j>=1;j--)
                for(int k=1;k<=j;k++)
                    f[j]=min(f[j],f[j-k]+a[i]*(long)pow(k,b[i]));

        cout.println(f[n]);
        cout.flush();
    }// End of main
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值