POJ 1276 (多重背包) Cash Machine

题意:

有n种纸币,已知每种纸币的面值和数量,求所能凑成的不超过cash的最大总面值。

分析:

这道题自己写了一下TLE了,好可耻。。

找了份比较简洁的代码抄过来了。。poj1276

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 const int maxn = 12;
 5 const int maxp = 100000 + 10;
 6
 7 bool vis[maxp];//是否到达总面值i
 8 int used[maxp];//到达总面值i时,该种纸币所用的数量
 9 int a[maxn], b[maxn];//每种纸币的数量以及面值
10
11 int cash, n;
12
13 int main()
14 {
15     //freopen("in.txt", "r", stdin);
16
17     while(scanf("%d", &cash) == 1)
18     {
19         scanf("%d", &n);
20         for(int i = 0; i < n; ++i) scanf("%d%d", &a[i], &b[i]);
21
22         memset(vis, false, sizeof(vis));
23         vis[0] = true;
24         for(int i = 0; i < n; ++i)
25         {
26             memset(used, 0, sizeof(used));
27             for(int j = b[i]; j <= cash; ++j)
28                 if(vis[j-b[i]] && !vis[j] && used[j-b[i]] < a[i])
29                 { vis[j] = true; used[j] = used[j-b[i]] + 1; }
30         }
31
32         for(int i = cash; i >= 0; --i) if(vis[i]) { printf("%d\n", i); break; }
33     }
34
35     return 0;
36 }

代码君

时间: 2024-07-31 01:28:52

POJ 1276 (多重背包) Cash Machine的相关文章

Cash Machine POJ - 1276 多重背包二进制优化

题意:多重背包模型  n种物品 每个m个  问背包容量下最多拿多少 这里要用二进制优化不然会超时 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=100000+10; 6 int dp[maxn]; 7 int w[500],c[50]; 8 int main(){ 9 10 int n,m,maxnum; 11 whi

poj 1276(多重背包)

Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31525   Accepted: 11333 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The m

Cash Machine poj (多重背包)

E - Cash Machine Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amo

hdu 1276 多重背包

735 3 4 125 6 5 3 350 //735的最大额,3种,4个125,6个5,3个350 633 4 500 30 6 100 1 5 0 1 735 0 0 3 10 100 10 50 10 10 735 630 0 0 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #inc

POJ 3260 多重背包+完全背包

前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168.1.1就能看到他的路由器的一切信息,包括密码,然后打开笔记本……好了,废话不多说,能连上网后第一时间当然是继续和队友之前约好的训练了. 今天翻看到之前落下的一道混合背包题目,然后在草稿本上慢慢地写递推方程,把一些细节细心地写好…(本来不用太费时间的,可是在家嘛,一会儿妈走来要我教她玩手机,一会儿有一个亲戚朋

POJ 1276 完全背包

---恢复内容开始--- 这道题就是明显的完全背包题啦,只不过物品的重量和价值是一样的. 我就是按照<背包九讲>中完全背包的思路,先把物品的数量 ni 按照1,2,4,8...的规律分解,然后直接用简单背包暴力 数据比较小,10种物品,每一种最多1000个,cash<=100000,这样子复杂度就是 10*log(1000)*100000,10的7次方刚好能过 下面是我的代码 1 #include<stdio.h> 2 #include<iostream> 3 u

poj1276——dp,多重背包

poj1276——dp,多重背包 Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28826   Accepted: 10310 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested c

POJ1276:Cash Machine(多重背包)

题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <math.h> using namespace std; int V,n,w

poj1742 多重背包的可行性问题

http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop