java对象组合,Java:基于字段获取大型对象列表的最有效组合

I'm looking to maximise the number of stars given a certain budget and max limit on the combination...

Example question:

With a budget of 500 euro, visiting only the maximum allowed restaurants or less, dine and collect the most stars possible.

I'm looking to write an efficient algorithm, that could potentially process 1 million Restaurant instances for up to 10 maxRestaurants...

Can anyone help attempt the problem?

Note: This is not homework. I intentionally left the attempt empty as I don't want to influence the efficiency of the solution

Restaurant.java

public class Restaurant {

double cost;

int stars;

public Restaurant(double cost, int stars) {

this.cost = cost;

this.stars = stars;

}

}

Main.java

import java.util.List;

import java.util.Arrays;

class Main {

public static void main(String[] args) {

Restaurant r1 = new Restaurant(100.0, 5);

Restaurant r2 = new Restaurant(20.0, 1);

Restaurant r3 = new Restaurant(75.0, 3);

Restaurant r4 = new Restaurant(125.0, 4);

Restaurant r5 = new Restaurant(60.0, 2);

Restaurant r6 = new Restaurant(80.0, 4);

Restaurant r7 = new Restaurant(40.0, 1);

Restaurant r8 = new Restaurant(200.0, 3);

Restaurant r9 = new Restaurant(120.0, 3);

Restaurant r10 = new Restaurant(50.0, 2);

List restaurants =

Arrays.asList(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10);

double budget;

int maxRestaurants;

budget = 500.0;

maxRestaurants = 1;

// { r1 } -- 5 stars

budget = 200;

maxRestaurants = 2;

// { r1, r6 } -- 9 stars

budget = 500;

maxRestaurants = 5;

// { r1, r4, r6, r3, r9 } -- 19 stars

budget = 200;

maxRestaurants = 10;

// { r1, r6, r2 } -- 10 stars

}

}

解决方案

To model it as such, define the weight vector of a restaurant as (price, 1) where price is the price of dining at that restaurant. The limits are (budget, maxRestaurants), and the payoff is the sum of stars over the restaurants chosen.

The knapsack problem, and most variants of it including 0-1 knapsack and multidimensional knapsack, are NP-hard. This means no known algorithm gives exact answers while also scaling well to large input sizes (such as 10 million).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值