Wedding shopping - UVa 11450 背包+滚动数组
来源:互联网 发布:蓝月传奇官印数据 编辑:程序博客网 时间:2024/05/21 10:21
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
3100 43 8 6 42 5 104 1 3 3 74 50 14 23 820 33 4 6 82 5 104 1 3 5 55 33 6 4 82 10 64 7 3 1 7
Sample Output
7519no solution
题意:你手上有M数量的钱,然后你有C种物品需要买,每种都要买一个,如果可以的话,问你最多花费多少钱,否则输出no solution。
思路:滚动数组,数组num1[i]表示用数量为i的钱能不能恰好买到前面所有种类的物品各一样,num2[i]记录加上这种物品之后的状态。
AC代码如下:
#include<cstdio>#include<cstring>using namespace std;int num1[210],num2[210];int main(){ int t,n,m,i,j,k,value,ans; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(num2,0,sizeof(num2)); num2[0]=1; while(m--) { scanf("%d",&k); for(i=0;i<=n;i++) { num1[i]=num2[i]; num2[i]=0; } while(k--) { scanf("%d",&value); for(i=n;i>=value;i--) if(num1[i-value]==1) num2[i]=1; } } ans=-1; for(i=0;i<=n;i++) if(num2[i]==1) ans=i; if(ans==-1) printf("no solution\n"); else printf("%d\n",ans); }}
- Wedding shopping - UVa 11450 背包+滚动数组
- UVa 11450 - Wedding shopping
- uva 11450 Wedding Shopping (DP)
- UVa 11450 Wedding shopping (DP)
- UVA 11450 Wedding shopping(DP)
- 11450 - Wedding shopping
- Wedding UVA
- UVa 11294 - Wedding
- UVA 11294 Wedding
- 01背包(滚动数组)
- 采药-01背包+滚动数组
- uva 11294 - Wedding(TwoSAT)
- UVA 11294 POJ 3648 Wedding
- UVA - 11294 Wedding(2-SAT)
- Uva 11294 Wedding(2-SAT)
- UVA 11294 Wedding(2-sat)
- POJ 2184(01背包+滚动数组)
- hdu 1203 01背包 滚动数组
- android账本-添加listview,列头
- ubuntu12.04的vim配置
- 解决cocos2d-x pvr.ccz格式的图片有时无法读取的一个bug
- Windows客户端C/C++编程规范“建议”——结构
- 李安:这世界上唯一扛得住岁月摧残的就是才华
- Wedding shopping - UVa 11450 背包+滚动数组
- Android中一次性安装多个应用。
- 1+1/2+1/3+1/4+.....+1/20的和
- AFNetworking 与ASIHTTPRequest的比较
- hibernate的注解属性mappedBy详解
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- VC++中用API调用对话框资源
- ajax json 处理返回值方法,附带中文乱码解决方案
- boj429.学姐的数码管-输出格式以及代码风格的考量