POJ1190

刚开始做的时候把Hi>Hi+1忽略了,导致结果错误,而且剪枝不够彻底,要注意使用剩余的部分与当前结果相结合来剪枝。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class BirthdayCake2 {

static int[][] minvs;
static int n;
static int m;
static int mins = 10000000;

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    minvs = new int[21][2];
    minvs[1][0] = 1;
    minvs[1][1] = 2;
    for (int i = 2; i < 21; i++) {
        minvs[i][0] = minvs[i - 1][0] + i * i * i;
        minvs[i][1] = minvs[i - 1][1] + 2 * i * i;
    }
    @SuppressWarnings("resource")
    Scanner sc = new Scanner(System.in);
    sc = new Scanner(new File("files/birthdaycake"));
    n = sc.nextInt();
    m = sc.nextInt();
    for (int i = 71; i >= m; i--) {
        for (int j = 30; j >= m; j--) {
            if (n - i * i * j >= 0)
                dfs(i, j, n - i * i * j, m - 1, i * i + 2 * i * j);
        }
    }
    if (mins == 10000000)
        System.out.println(0);
    else
        System.out.println(mins);
}

private static void dfs(int prer, int preh, int nn, int mm, int cs) {
    // TODO Auto-generated method stub
    if (mm == 0) {
        if (nn == 0 && cs < mins)
            mins = cs;
        return;
    }
    if (nn <= 0 || cs + minvs[mm][1] >= mins || 2 * nn / prer + cs >= mins
            || nn < minvs[mm][0])
        return;
    if (mm == 1) {
        if ((prer - 1) * (prer - 1) * (preh - 1) < nn)
            return;
        for (int r = prer - 1; r > 0; r--) {
            if (nn % (r * r) == 0 && nn / (r * r) < preh)
                if (cs + 2 * nn / r < mins) {
                    mins = cs + 2 * nn / r;
                    break;
                }
        }
    } else {
        for (int i = prer - 1; i >= mm; i--) {
            for (int j = preh - 1; j >= mm; j--) {
                dfs(i, j, nn - i * i * j, mm - 1, cs + 2 * i * j);
            }
        }
    }
}

}

sample input:
10000
8

sample output:
1532

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值