UV 11450 wedding shopping (DP)

Source: Internet
Author: User

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 received
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:
Shirt, a belt, some shoes, a tie, etce.pdf.
We are offered different models for each class
Garment (for example, three shirts, two belts, four
Shoes,...). We have to buy one model of each class
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 shoshould 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 [I] [J] indicates that I have been purchased. j

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>typedef long long LL;using namespace std;int dp[25][220];int k[25],pri[25][25];int m,c;int main(){    int t;    cin>>t;    while(t--)    {        cin>>m>>c;        for(int i=1;i<=c;i++)        {            cin>>k[i];            for(int j=1;j<=k[i];j++)                cin>>pri[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 kk=1;kk<=k[i];kk++)                    {                        if(j+pri[i][kk]<=m)                            dp[i][j+pri[i][kk]]=1;                    }                }            }        }        int flag=1;        for(int i=m;i>=0;i--)        {            if(dp[c][i])            {                cout<<i<<endl;                flag=0;                break;            }        }        if(flag)   cout<<"no solution"<<endl;    }    return 0;}



UV 11450 wedding shopping (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.