24h購物| | PChome| 登入
2013-09-11 08:33:11| 人氣1,351| 回應0 | 上一篇 | 下一篇

[UVA][dp] 10645 - Menu

推薦 0 收藏 0 轉貼0 訂閱站台

 Menu

 

Input: standard input

Output: standard output

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;

}

台長: Morris
人氣(1,351) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][dp] 11285 - Exchange Rates
此分類上一篇:[UVA][dp] 10604 - Chemical Reaction

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文