11450 - Wedding shopping

 D. Wedding Shopping 

Background

One of our best friends is getting married and we all are nervous because he is thefirst 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 famousdepartment 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 justone.

As our budget is limited, we cannot spend more money than it, but we want to spend themaximum possible. It's possible that we cannot buy one model of each class of garment dueto the short amount of money we have.

The Input

The first line of the input contains an integer, N, indicating the numberof 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 Cis 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 firstinteger, K (1<=K<=20), indicates the number of different modelsfor each garment and it is followed by K integers indicating the price of eachmodel of that garment.

The Output

For each test case, the output should consist of one integer indicating the maximumamount of money necessary to buy one element of each garment without exceeding the initialamount 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
#include<stdio.h>
#include<string.h>
int a[25][25],f[25][205],b[25],t,m,c,i,j,k;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		memset(f,0,sizeof(f));**f=1;
		scanf("%d%d",&m,&c);
		for(i=1;i<=c;i++)
		{
			scanf("%d",&b[i]);
			for(j=1;j<=b[i];j++)
				scanf("%d",&a[i][j]);
		}
		for(i=1;i<=c;i++)  
			for(j=0;j<=m;j++)  
				if(f[i-1][j])  
					for(k=1;k<=b[i];k++)  
						if(j+a[i][k]<=m)  
							f[i][j+a[i][k]]=1;  
		int max=0;  
		for(i=m;i>=1;i--)  
			if(f[c][i])	{max=i;break;}  
			if (max) printf("%d\n",max);  
			else puts("no solution");  
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值