度度熊的午饭时光

度度熊的午饭时光

Accepts: 823
Submissions: 9241
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description

度度熊最期待每天的午饭时光,因为早饭菜品清淡,晚饭减肥不敢吃太多(胖纸的忧伤T.T)。

百度食堂的午餐超级丰富,祖国各大菜系应有尽有,度度熊在每个窗口都有爱吃的菜品,而且他还为喜爱的菜品打了分,吃货的情怀呀(>.<)。

但是,好吃的饭菜总是很贵,每天的午饭预算有限,请帮度度熊算一算,怎样打饭才能买到的最好吃的饭菜?(不超过预算、不重样、午餐等分最高的情况下,选择菜品序号加和最小,加和相等时字典序最小的组合)

Input

第一行一个整数T,表示T组数据。每组测试数据将以如下格式从标准输入读入:

B

N

score_1 cost_1

score_2 cost_2

:

score_N cost_N

第一行,正整数B(0 <= B <= 1000),代表午餐的预算。

第二行,正整数N (0 <= N <= 100),代表午餐可选的菜品数量

从第三行到第 (N + 2) 行,每行两个正整数,以空格分隔,score_i表示菜品的得分,cost_i表示菜品的价格(0 <= score_i, cost_i <= 100)。

Output

对于每组数据,输出两行:第一行输出:"Case #i:"。i代表第i组测试数据。第二行输出菜品的总得分和总花费,以空格分隔。第三行输出所选菜品的序号,菜品序号从1开始,以空格分隔。

Sample Input
2
29
6
9 10
3 4
6 5
7 20
10 9
15 11
0
2
2 23
10 12
Sample Output
Case #1:
34 29
2 3 5 6
Case #2:
0 0


开始我是不会的,现在还是不怎么会,只好看题解了
其实是个01 背包加回溯,



#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cctype>
#include <fstream>
#define INF 0x3f3f3f3f
#define TEST cout<<"stop here"<<endl
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;

struct meal{
    int score,cost;
}a[110];
bool ans[110];
int dp[1010];
bool vis[110][1010];
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int T,kase=1;
    cin>> T;
    while(T--){
        int b,n;
        cin>>b>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i].score>>a[i].cost;

        memset(dp,0,sizeof(dp));
        memset(vis,false,sizeof(vis));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++){
            for(int j=b;j>=a[i].cost;j--){
                if(dp[j] < dp[j-a[i].cost] + a[i].score){
                    dp[j] = dp[j-a[i].cost] + a[i].score;
                    vis[i][j] = true;
                }
                else vis[i][j] = false;
            }
        }

        int p=b,cnt = 0;
        for(int i=n;i>=1;i--){
          if(vis[i][p]){
                ans[i]=true;
                p -= a[i].cost;
                cnt++;
            }
        }
        int ANS = 0;
        for(int i=1;i<=n;i++){
            if(ans[i])
                ANS += a[i].cost;
        }
        printf("Case #%d:\n",kase++);
        printf("%d %d\n",dp[b],ANS);
        for (int i = 1; i <= n; i++)
            if (ans[i])
                printf("%d%c", i, (--cnt == 0) ? '\n' : ' ');

    }
    return 0;
}

 

posted @ 2017-08-07 16:30  haha1008  阅读(331)  评论(0编辑  收藏  举报