24h購物| | PChome| 登入
2012-02-27 14:38:36| 人氣1,888| 回應0 | 上一篇 | 下一篇

[UVA] 11450 - Wedding shopping

推薦 0 收藏 0 轉貼0 訂閱站台

 D. Wedding Shopping 

Background

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.

The Problem

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.

The 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.

The 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



做法 : DP 背包問題的變形

#include <stdio.h>
int main() {
int T, M, C, K, i, j, k;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &M, &C);
int DP[201] = {}, W[201] = {}, A[20];
for(i = 0; i < C; i++) {
scanf("%d", &K);
for(j = 0; j < K; j++)
scanf("%d", &A[j]);
for(j = M; j >= 0; j--) {
int max = 0;
for(k = 0; k < K; k++) {
if(j-A[k] >= 0) {
if(max <= DP[j-A[k]]+A[k] && W[j-A[k]] == i)
max = DP[j-A[k]]+A[k];
}
}
if(max) W[j]++;
DP[j] = DP[j] > max ? DP[j] : max;

}
}
int max = 0;
for(i = 0; i <= M; i++) {
if(W[i] == C)
max = max > DP[i] ? max : DP[i];
}
if(max == 0)
puts("no solution");
else
printf("%d\n", max);
}
return 0;
}
 

台長: Morris
人氣(1,888) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10827 - Maximum sum on a torus
此分類上一篇:[UVA] 11407 - Squares

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文