UVA 11450 Wedding shopping(DP)

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

dp[i][j]表示已经买了i种花费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;
}
时间: 2024-10-25 22:21:47

UVA 11450 Wedding shopping(DP)的相关文章

UVA 10641 - Barisal Stadium(DP + 几何)

题目链接:10641 - Barisal Stadium 题意:逆时针给定n个点,在给m个灯,每个灯有一个花费,要求最小花费使得所有边能被灯照到 思路:用向量叉积判断向量的顺逆时针关系,从而预处理出每个灯能照到的边,然后由于n个点是环的,所以可以直接扩大两倍,dp时候去枚举起点即可 状态为dp[i]表示现在照到i条边之前的边全部照亮需要的最小花费 代码: #include <stdio.h> #include <string.h> const double eps = 1e-6;

UVA 580 - Critical Mass(DP)

题目链接:580 - Critical Mass 题意:一个栈,里面可以放L和U,有三个连续的U就是不安全的,问共有几种不安全的情况 思路:dp,dp[i][j][k],表示放到第i个,最后两个状态为j,k表示有没有出现不安全.然后去记忆化搜索一下就可以了 然后还有一种做法是,先考虑安全的情况,在用总情况(1<<n 种)减去安全的情况,安全的情况的递推方式很容易dp[i]表示放第i个, dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]. 不过这题都没给数据范围

UVA 11578 - Situp Benches(dp)

题目链接:11578 - Situp Benches 题意:健♂身♂房有两个仰卧起坐坐垫,每次调整角度要花费10元/10度,每次使用要花费15,现在给定n个人的时间顺序,和所希望的角度,求最少花费 思路:dp,dp[i][j][k]表示第i个人,一个角度为j,另一个为k的最小花费,一个人用和两个人用的情况分开讨论,然后记录dp状态转移路径.这个输出路径让这题变得麻烦了不少.不过机智的我还是把它搞♂出♂来♂了. 代码: #include <stdio.h> #include <string

uva 10163 Storage Keepers (DP)

uva 10163 Storage Keepers 题目大意:有N个仓库,M个管理员,M个管理员每个人的工资都不一样,工资与他们的能力值(P)相同.一个管理员可以看管多个(n)仓库,但是仓库的安全值就会变为P / n.现在要是的最小的安全值最大,并且还要求出该状况下的最小花费. 解题思路:两次DP,第一次dp求出最大的最小安全值ans,第二次dp根据第一次dp求出的ans求出最小的花费. #include <cstdio> #include <cstring> #include &

uva 10003 Cutting Sticks (DP)

uva 10003 Cutting Sticks Description 你的任务是替一家叫Analog Cutting Machinery (ACM)的公司切割木棍.切割木棍的成本是根据木棍的长度而定.而且切割木棍的时候每次只切一段.很显然的,不同切割的顺序会有不同的成本.例如:有一根长10公尺的木棍必须在第2.4.7公尺的地方切割.这个时候就有几种选择了.你可以选择先切2公尺的地方,然后切4公尺的地方,最后切7公尺的地方.这样的选择其成本为:10+8+6=24.因为第一次切时木棍长10公尺,

uva 10617 Again Palindrome (DP)

uva 10617 Again Palindrome 题目大意:给出一段字符串,可进行删除操作,可以删除任意位置任意个数(可以是0)的字符.问,进行删除操作使原本字符串变成回文字符串,有几种方式. 解题思路: dp[i][j] = 1 (i == j),单独一个字符也是回文字符串 s[i] != s[j]时, dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1], dp[i + 1][j] 和 dp[i][j + 1]的公共部分dp[

uva 116 Unidirectional TSP (DP)

uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Traveling Sa

UVA 1619 Feel Good(DP)

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. A new idea Bill has recently developed assigns a non-negat

UVa 12186 Another Crisis (DP)

题意:有一个老板和n个员工,除了老板每个员工都有唯一的上司,老板编号为0,员工们为1-n,工人(没有下属的员工),要交一份请愿书, 但是不能跨级,当一个不是工人的员工接受到直系下属不少于T%的签字时,自己也会签字,并交给上级,问你最少有多少工人签字,才能让老板收到请愿书. 析:题意比较简单,也好理解,很明显是一个动态规划的题目,d(u)表示u给上级要发信至少需要多少工人签字.假设u有k个结点,那么至少要 c = (kT-1)/100 + 1个工人,然后把每个结点排序,找出最少的工人即可,挺简单的