UVa 11308 - Bankrupt Baker

时间:2023-03-09 07:13:08
UVa 11308 - Bankrupt Baker

  题目大意:给出一些原料和价钱和若干份菜谱,每份菜谱都标明所需的原料和数量,找出所有不超过预算的菜谱。

  没什么好说的,主要是对map的运用。

 #include <cstdio>
#include <string>
#include <map>
#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;
typedef map<string, int> msi; struct Recipe
{
string name;
int cost;
bool operator < (const Recipe& r) const
{
if (cost != r.cost) return cost < r.cost;
return name < r.name;
}
}recipe[]; int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
cin >> T;
getchar();
msi ingredient;
while (T--)
{
string title;
getline(cin, title);
transform(title.begin(), title.end(), title.begin(), ::toupper);
int m, n, b;
cin >> m >> n >> b;
getchar();
ingredient.clear();
string str;
int x;
for (int i = ; i < m; i++)
{
cin >> str >> x;
getchar();
ingredient[str] = x;
}
for (int i = ; i < n; i++)
{
getline(cin, recipe[i].name);
recipe[i].cost = ;
int k;
cin >> k;
getchar();
for (int j = ; j < k; j++)
{
cin >> str >> x;
getchar();
recipe[i].cost += ingredient[str]*x;
}
}
sort(recipe, recipe+n);
cout << title << endl;
if (recipe[].cost > b)
{
cout << "Too expensive!" << endl << endl;
continue;
}
for (int i = ; i < n; i++)
{
if (recipe[i].cost > b) break;
cout << recipe[i].name << endl;
}
cout << endl;
}
return ;
}