[UVA][dp] 10645 - Menu
Input:
standard input
Time Limit: 10 seconds
Alfred
wants to plan what to cook in the next days. He can cook various
dishes.
For each dish the costs of the ingredients and the benefit value is
known. If a
dish is cooked the second time in a row, the benefit value for the
second time
is 50 percent of the benefit value of first time, if it is prepared for
the
third or higher time in a row, the benefit value is 0. For example
cooking a
dish with benefit value v three times in a row leads to a total benefit
value of 1.5*v.
Help him to build the menu which maximizes the benefit value under the
constraint
that his budget is not exceeded.
Input
The input consists of several test cases. Each test case
begins with 3 integers in a line: The number of days k (1<=k<=21) Alfred wants
to plan for, the number of dishes n (1<=n<=50) he can cook and his budget m
(0<=m<=100).
The following n lines describe the dishes Alfred can cook. The i-th
line contains two integers: the costs c
(1<=c<=50) and the benefit value v (1<=v<=10000) of the
i-th dish.
The end of the input is signaled by a test case with k = n = m = 0.
You don't
need to process this test case.
Output
For each output, print the maximum benefit value reachable with 1 digit
after the decimal point. Then print k integers
with i-th integer being the number of the dish to cook on day i. Dishes
are
numbered from 1 to n. Print at least one space or new line character
after each
integer.
If there are several possible menus reaching the maximum benefit value,
select
the one
with minimum costs, if there are several with minimum costs, you can
print any of them.
If every menu exceeds the budget, print only the benefit value of 0.
Sample Input
2 1 5
3 5
3 5 20
2 5
18 6
1 1
3 3
2 3
0 0 0
Sample
Output
0.0
13.0
1 5 1
Problem setter: Adrian Kuegel
題目描述:
有 N 天要準備食物,每天準備一種。每種食物都有花費和利潤,如果連續兩天煮同一種,
則第二天的利潤會減半,而如果連續三天以上,則第二天減半,第三天之後則沒有利潤。
問 N 天在限制的花費下,最高利潤為何?並把方法列出來。
題目解法:
背包 dp,不考慮使用滾動數組,因為要把方法列出來。
dp[i][j][k][w] 表示第 i 天使用 j 號食物,此時該食物已經使用連續 k+1 天,
w 則用來使用背包 dp,表示花費 w 的最大利潤。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
double dp[22][50][2][105];
struct node {
int dish;
node *back;
};
node argvdp[22][50][2][105];
//[days][previous days dish][<- continous count-1][];
int main() {
int K, N, M;
int cost[105], benefit[105];
int i, j, k, p, q, r;
while(scanf("%d %d %d", &K, &N, &M) == 3 && K+N+M) {
for(i = 0; i < N; i++)
scanf("%d %d", &cost[i], &benefit[i]);
for(i = 0; i <= K; i++)
for(j = 0; j < N; j++)
for(k = 0; k < 2; k++)
for(p = 0; p <= M; p++)
dp[i][j][k][p] = -1;
dp[0][0][0][0] = 0;
argvdp[0][0][0][0].dish = -1;
for(i = 0; i < K; i++) {
for(j = 0; j < N; j++) {
for(k = 0; k < 2; k++) {
for(p = 0; p <= M; p++) {
if(dp[i][j][k][p] == -1)
continue;
for(q = 0; q < N; q++) {
if(cost[q]+p > M) continue;
double bb;
int argv;//count
if(i == 0) bb = benefit[q], argv = 0;
else if(j == q && k == 0)
bb = benefit[q]/2.0, argv = 1;
else if(j != q)
bb = benefit[q], argv = 0;
else
bb = 0, argv = 1;
if(dp[i+1][q][argv][cost[q]+p] < dp[i][j][k][p]+bb) {
dp[i+1][q][argv][cost[q]+p] = dp[i][j][k][p]+bb;
argvdp[i+1][q][argv][cost[q]+p].dish = q;
argvdp[i+1][q][argv][cost[q]+p].back = &argvdp[i][j][k][p];
}
}
}
}
}
}
double mx = 0;
int mncost;
node *head;
for(i = 0; i < N; i++) {
for(j = 0; j < 2; j++) {
for(k = 0; k <= M; k++) {
if(dp[K][i][j][k] > mx)
mx = dp[K][i][j][k], mncost = M+1;
if(dp[K][i][j][k] == mx) {
if(k < mncost) {
mncost = k;
head = &argvdp[K][i][j][k];
}
}
}
}
}
printf("%.1lf\n", mx);
if(mx == 0.0) {
puts("");
} else {
vector<int> buf;
while(head->dish != -1) {
buf.push_back(head->dish);
head = head->back;
}
for(i = buf.size()-1; i >= 0; i--) {
printf("%d", buf[i]+1);
if(i) putchar(' ');
}
puts("");
}
}
return 0;
}