UVa10645 - Menu

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 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int ndy,nds,bgt;
double dp[101][51][22][22];
const double inf = 1e16;
struct dish{
    int cost;
    double val;
    dish(int cost=0,double val=0):cost(cost),val(val){}
};
vector<dish> vd;
void init(){
    vd.clear();
    for(int a = 0; a < 101; a++)
        for(int b = 0; b < 51; b++)
            for(int c = 0; c < 22; c++)
                for(int d = 0; d < 22; d++)
                    dp[a][b][c][d] = -1;
}
double dfs(int bud,int dish,int day,int times){
    if(bud < 0) return -inf;
    if(day==ndy)return 0.0;
    if(dp[bud][dish][day][times]!=-1) return dp[bud][dish][day][times];
    double ans = -inf;
    if(times==1) ans = dfs(bud-vd[dish].cost,dish,day+1,times+1)+vd[dish].val*0.5;
    else ans = dfs(bud-vd[dish].cost,dish,day+1,times+1);
    for(int i = 0; i < nds; i++){
        if(i!=dish)
            ans = max(ans,dfs(bud-vd[i].cost,i,day+1,1)+vd[i].val);
    }
    return dp[bud][dish][day][times] = ans;
}
void print(int bud,int dish,int day,int times){

    if(bud < 0) return;
    if(day==ndy) return;
    int pos = dish;
    double ans = -1.0;
    if(times==1&&bud>=vd[dish].cost) ans = dp[bud-vd[dish].cost][dish][day+1][times+1]+vd[dish].val*0.5;
    if(bud>=vd[dish].cost&×>1) ans = dp[bud-vd[dish].cost][dish][day+1][1];
    for(int i = 0; i < nds; i++){
        if(i!=dish&&bud>=vd[i].cost)
            if(ans < dp[bud-vd[i].cost][i][day+1][1]+vd[i].val){
                pos = i;
                ans = dp[bud-vd[i].cost][i][day+1][1]+vd[i].val;
            }else if(ans == dp[bud-vd[i].cost][i][day+1][1]+vd[i].val){
                if(vd[i].cost<vd[pos].cost){
                    pos = i;
                }
            }
    }
    cout<<pos+1<<" ";
    if(pos == dish){
        print(bud-vd[dish].cost,dish,day+1,times+1);
    }else{
        print(bud-vd[pos].cost,pos,day+1,1);
    }
    return;
}
int main(){
	//freopen("in","r",stdin);
    while(cin >> ndy >> nds >> bgt&&ndy+nds+bgt){
        init();
        for(int i = 0; i < nds; i++){
			int cost;
			double val;
			cin >> cost >> val;
			vd.push_back(dish(cost,val));
		}
        double ans = -1;
        int pos = 0;
        for(int i = 0; i < nds; i++){
            if(dfs(bgt-vd[i].cost,i,1,1)+vd[i].val>ans){
                pos = i;
                ans = dfs(bgt-vd[i].cost,i,1,1)+vd[i].val;
            }
        }
        if(ans < 0)  ans = 0;
        printf("%.1lf\n",ans);
        if(ans!=0.0){
            cout<<pos+1<<" ";
            print(bgt-vd[pos].cost,pos,1,1);
            cout<<endl;
        }


    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值