Ingredients(Gym - 101635E)

The chef of a restaurant aspiring for a Michelin star wants to display a selection of her signature dishes for inspectors. For this, she has allocated a maximum budget B for the cumulated cost, and she wants to maximize the cumulated prestige of the dishes that she is showing to the inspectors.
To measure the prestige of her dishes, the chef maintains a list of recipes, along with their costs and ingredients. For each recipe, a derived dish is obtained from a base dish by adding an ingredient. The recipe mentions two extra pieces of information: the cost of applying the recipe, on top of the cost of the base dish, and the prestige the recipe adds to the prestige of the base dish. The chef measures the prestige by her own units, called “prestige units.”
For example, a recipe list for making pizza looks like:
• pizza_tomato pizza_base tomato 1 2
• pizza_classic pizza_tomato cheese 5 5
Here, pizza_base is an elementary dish, a dish with no associated recipe, a dish so simple that its cost is negligible (set to 0) and its prestige also 0. The chef can obtain the derived dish pizza_tomato by adding the ingredient tomato to the base dish pizza_base, for a cost of 1 euro and a gain of 2 prestige units. A pizza_classic is obtained from a pizza_tomato by adding cheese, for an added cost of 5, and a prestige of 5 added to the prestige of the base dish; this means the total cost of pizza_classic is 6 and its total prestige is 7.
A signature dish selection could for instance include both a pizza_tomato and a pizza_classic. Such a selection would have cumulated total prestige of 9, and cumulated total cost of 7.
Armed with the list of recipes and a budget B, the chef wants to provide a signature dish selection to Michelin inspectors so that the cumulated total prestige of the dishes is maximized, keeping their cumulated total cost at most B.

Important Notes

• No dish can appear twice in the signature dish selection.
• Any dish that does not appear as a derived dish in any recipe is considered to be an elementary
dish, with cost 0 and prestige 0.
• A dish can appear more than once as a resulting dish in the recipe list; if there is more than one
way to obtain a dish, the one yielding the smallest total cost is always chosen; if the total costs
are equal, the one yielding the highest total prestige should be chosen.
• The recipes are such that no dish D can be obtained by adding one or more ingredients to D
itself.

Input

• The first line consists of the budget B, an integer.
• The second line consists of the number N of recipes, an integer.
• Each of the following N lines describes a recipe, as the following elements separated by single
spaces: the derived dish name (a string); the base dish name (a string); the added ingredient (a string); the added price (an integer); the added prestige (an integer).
10
Limits
• 0��<=B<=��10000;
• 0<=��N<=��1000000;
• there can be at most 10 000 different dishes (elementary or derived);
• costs and prestiges in recipes are between 1 and 10 000 (inclusive);
• strings contain at most 20 ASCII characters (letters, digits, and ’_’ only).

Output

The output should consist of two lines, each with a single integer. On the first line: the maximal cumulated prestige within the budget. On the second line: the minimal cumulated cost corresponding to the maximal cumulated prestige, necessarily less than or equal to the budget.

Sample Input

15
6
pizza_tomato pizza_base tomato 1 2
pizza_cheese pizza_base cheese 5 10
pizza_classic pizza_tomato cheese 5 5
pizza_classic pizza_cheese tomato 1 2
pizza_salami pizza_classic salami 7 6
pizza_spicy pizza_tomato chili 3 1

Sample Output

 25 
 15

题意:

给出最大成本B,以及N条菜谱,每条菜谱代表一种成品是由一种原料加上辅料制作而成,需要消耗a的代价产生b的声望。辅料是现成的,要求用最小的成本产生最大的声望。

思路:

如果已知每种成品的代价和声望的话很明显这就是一个01背包的题,现在给出了N条路线,那么只需要跑一遍最短路就可以得出最小代价和最大声望,由于有多个起点,可以把所有起点连接到一个超级源点上,或者对每一个起点跑一次单源最短路都可以。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 10;
const int mod = 1e9;
const int maxx = 1e6 + 10;
map <string, int> dic;
struct edge {
    int to, next, a, b;
    edge(int _to, int _next, int _a, int _b) {
        to = _to;
        next = _next;
        a = _a;
        b = _b;
    }
    edge() {}
} edges[maxx];
int tot, cnt;
int head[maxx], deg[maxn], dp[maxn];
int weight[maxn], value[maxn];
bool vis[maxn];
void add_edge(int u, int v, int a, int b) {
    edges[cnt] = {v, head[u], a, b};
    head[u] = cnt++;
}
int B, N;
void init() {
    memset(dp, 0, sizeof dp);
    memset(head, -1, sizeof head);
    memset(edges, 0, sizeof edges);
    memset(deg, 0, sizeof deg);
    tot = 1;
    cnt = 0;
}
void spfa(int s) {
    memset(vis, false, sizeof vis);
    memset(value, 0x3f3f3f3f, sizeof value);
    memset(weight, 0, sizeof weight);
    queue <int> q;
    q.push(s);
    vis[s] = true;
    value[s] = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for (int i = head[u]; i != -1; i = edges[i].next) {
            int v = edges[i].to;
            if (value[v] > value[u] + edges[i].a) {
                value[v] = value[u] + edges[i].a;
                weight[v] = weight[u] + edges[i].b;
                if (!vis[v]) {
                    vis[v] = true;
                    q.push(v);
                }
            }
            else if (value[v] == value[u] + edges[i].a) {
                if (weight[v] < weight[u] + edges[i].b) {
                    weight[v] = weight[u] + edges[i].b;
                    if (!vis[v]) {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> B >> N;
    init();
    string str1, str2, str3;
    int a, b;
    while (N--) {
        cin >> str1 >> str2 >> str3 >> a >> b;
        if (!dic.count(str1)) dic[str1] = tot++;
        if (!dic.count(str2)) dic[str2] = tot++;
        deg[dic[str1]]++;
//        deg[dic[str2]]++;
        add_edge(dic[str2], dic[str1], a, b);
    }
    for (int i = 1; i < tot; i++) {
        if (!deg[i]) add_edge(0, i, 0, 0);
    }
    spfa(0);
    for (int i = 1; i < tot; i++)
        for (int j = B; j >= value[i]; j--) {
            dp[j] = max(dp[j], dp[j - value[i]] + weight[i]);
        }
    int V = 0, W = 0;
    for (int i = 0; i <= B; i++) {
        if (dp[i] > W) {
            W = dp[i];
            V = i;
        }
    }
    printf("%d\n", W);
    printf("%d\n", V);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值