UVA11450 Wedding shoppingDP
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11450 Wedding shoppingDP相关的知识,希望对你有一定的参考价值。
One of our best friends is getting married and we all are nervous because he is the first of us who is doing something similar. In fact, we have never assisted to a wedding, so we have no clothes or accessories, and to solve the problem we are going to a famous department store of our city to buy all we need: a shirt, a belt, some shoes, a tie, etcetera.
We are offered different models for each class of garment (for example, three shirts, two belts, four shoes, …). We have to buy one model of each class of garment, and just one.
As our budget is limited, we cannot spend more money than it, but we want to spend the maximum possible. It’s possible that we cannot buy one model of each class of garment due to the short amount of money we have.
Input
The first line of the input contains an integer,N, indicating the number of test cases. For each test case, some lines appear, the first one contains two integers, M and C, separated by blanks (1 ≤ M ≤ 200, and 1 ≤ C ≤ 20), where M is the available amount of money and C is the number of garments you have to buy. Following this line, there are C lines, each one with some integers separated by blanks; in each of these lines the first integer, K (1 ≤ K ≤ 20), indicates the number of different models for each garment and it is followed by K integers indicating the price of each model of that garment.
Output
For each test case, the output should consist of one integer indicating the maximum amount of money necessary to buy one element of each garment without exceeding the initial amount of money. If there is no solution, you must print ‘no solution’.
Sample Input
3
100 4
3 8 6 4
2 5 10
4 1 3 3 7
4 50 14 23 8
20 3
3 4 6 8
2 5 10
4 1 3 5 5
5 3
3 6 4 8
2 10 6
4 7 3 1 7
Sample Output
75
19
no solution
问题链接:UVA11450 Wedding shopping
问题简述:计算买东西的最大价值。
问题分析:动态规划问题,类似于背包问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA11450 Wedding shopping */
#include <bits/stdc++.h>
using namespace std;
const int M = 200 + 1, K = 20 + 1;
int price[K], dp[M], w[M];
int main()
{
int t, m, c, k;
scanf("%d", &t);
while (t--) {
memset(dp, 0, sizeof dp);
memset(w, 0, sizeof w);
scanf("%d%d", &m, &c);
for (int i = 0; i < c; i++) {
scanf("%d", &k);
for (int j = 0; j < k; j++)
scanf("%d", &price[j]);
for (int j = m; j >= 0; j--) {
int maxp = 0;
for (int ki = 0; ki < k; ki++)
if (j - price[ki] >= 0) {
if (maxp <= dp[j - price[ki]] + price[ki] && w[j - price[ki]] == i)
maxp = dp[j - price[ki]] + price[ki];
}
if (maxp) w[j]++;
dp[j] = max(dp[j], maxp);
}
}
int maxans = 0;
for (int i = 0; i <= m; i++)
if (w[i] == c) maxans = max(maxans, dp[i]);
if (maxans) printf("%d\\n", maxans);
else printf("no solution\\n");
}
return 0;
}
AC的C++语言程序如下:
/* UVA11450 Wedding shopping */
#include <bits/stdc++.h>
using namespace std;
const int M = 200 + 1, C = 20 + 1, K = 20 + 1;
int k[C], price[C][K], dp[K][M];
int main()
{
int t, m, c;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &m, &c);
for (int i = 1; i <= c; i++) {
scanf("%d", &k[i]);
for (int j = 1; j <= k[i]; j++)
scanf("%d", &price[i][j]);
}
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for (int i = 1; i <= c; i++)
for (int j = 0; j <= m; j++)
if (dp[i - 1][j])
for (int ki = 1; ki <= k[i]; ki++)
if (j + price[i][ki] <= m)
dp[i][j + price[i][ki]] = 1;
int maxans = 0;
for (int i = m; i >= 1; i--)
if (dp[c][i]) {
maxans = i;
break;
}
if (maxans) printf("%d\\n", maxans);
else printf("no solution\\n");
}
return 0;
}
以上是关于UVA11450 Wedding shoppingDP的主要内容,如果未能解决你的问题,请参考以下文章