POJ1742可行性背包

题目:

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. He wanted to pay the
exact price(without change) and he known the price would not more than m.But he didn‘t know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony‘s coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The
last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4

题目意思就是:

一定量的硬币,问可以凑出多少不同的价值。

之所以做这题,是因为在LTC的男人八题看到的。

之前其实真的没做过可行性背包,当时第一反应就是拍了一个优化的多重背包,结果果然是TLE了。之后看题解,原来有O(nm)的姿势,补上。

最近改用vim了!codeblocks,byebye!

周日就是GDCPC了,虽然队伍的缺陷非常多,但是,尽力吧!

/*************************************************************************
    > OS     : Linux 3.2.0-60-generic #91-Ubuntu
    > Author : yaolong
    > Mail   : [email protected]
    > Created Time: 2014年05月09日 星期五 10:23:35
 ************************************************************************/

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 105
#define MAXV 100005
int cnt[MAXN],val[MAXN];
int used[MAXV];
bool f[MAXV];
int n,V;
int coins;
/*可行性背包,当问题仅仅是问能否装到,那么只要达到过就不要再做了*/
void AMultipack(int val,int num){
    memset(used,0,sizeof(used));
    for(int j=val;j<=V;j++){
        if(!f[j]&&f[j-val]&&used[j-val]<num){
            //要求f[j]没有存在过,但是f[j-val]是之前达到的,且还可以购买
            f[j]=true;
            used[j]=used[j-val]+1;
            coins++;
        }
    }
}
int main(){
    int n;
    int i,j,k;
    while(scanf("%d%d",&n,&V)&&(n||V)){

        for(i=0;i<n;i++){
            scanf("%d",val+i);
        }
        for(i=0;i<n;i++){
            scanf("%d",cnt+i);
        }
        coins=0;
        memset(f,0,sizeof(f));
        f[0]=true;
        for(i=0;i<n;i++){
            AMultipack(val[i],cnt[i]);
        }
        printf("%d\n",coins);
    }
    return 0;
}

POJ1742可行性背包

时间: 2024-10-14 12:26:29

POJ1742可行性背包的相关文章

Miku and Generals(可行性背包) 西安邀请赛

 题目链接: https://nanti.jisuanke.com/t/39271  题目大意: 当前有两个人,然后有n个点,每个点都有权值.然后给你m个对应关系,每一次的对应关系给你两个数,t1 和 t2 ,代表这两个不能在一个人手里.然后让你分配这n个点,使得这两个人的 差距进尽可能的小.然后输出大的那个. 具体思路: 对于每一个联通图,我们二分图染色,dp[i]表示当前这个差值能不能到达,这个过程可以通过可行性背包来解决.然后找一个最小的差值就可以了. AC代码: 1 #include<b

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

[tyvj1190]积木城堡

描述 XC的儿子小XC最喜欢玩的游戏用积木垒漂亮的城堡.城堡是用一些立方体的积木垒成的,城堡的每一层是一块积木.小XC是一个比他爸爸XC还聪明的孩子,他发现垒城堡的时候,如果下面的积木比上面的积木大,那么城堡便不容易倒.所以他在垒城堡的时候总是遵循这样的规则.小XC想把自己垒的城堡送给幼儿园里漂亮的女孩子们,这样可以增加他的好感度.为了公平起见,他决定把送给每个女孩子一样高的城堡,这样可以避免女孩子们为了获得更漂亮的城堡而引起争执.可是他发现自己在垒城堡的时候并没有预先考虑到这一点.所以他现在要

CONTEST36 小Z的模拟赛(2)

A.小Z的可恶路障 题目:http://www.luogu.org/problem/show?pid=U126 题解:暴力也可以过吧.我为了保险先求了一次最短路,然后枚举这条最短路上的所有边... 代码: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream&

dp优化简单总结

1.二分优化 (使用二分查找优化查找效率) 典型例题:LIS dp[i]保存长度为 i 的上升子序列中最小的结尾,可以用二分查找优化到nlogn 2.数学优化 (通过数学结论减少状态数) 例题1:hdu4623   题解 例题2:usaco4.11 题解 大意是求10个数及其倍数最大不能表示的数 有数论结论证明对于互质的p,q,最大不能表示的数不会超过p*q,所以这个题就成了有上限(256*256)的问题了,在上限内跑背包即可. 3.矩阵优化(通过矩阵快速幂加速状态转移) ...... 4.单调

Loj515 「LibreOJ β Round #2」贪心只能过样例 - Bitset,Dp

bitset的基本应用了 类似可行性背包的dp考虑 复杂度O(nmL/64) 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 bitset <1000005> bs,bs0; 5 6 int n,a,b; 7 8 int main(){ 9 ios::sync_with_stdio(false); 10 cin>>n; 11 bs[0]=1; 12 for(int i=1;i<=n;i++){ 13 bs

POJ1742 Coins[多重背包可行性]

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 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

【DP|多重背包可行性】POJ-1014 Dividing

Dividing Time Limit: 1000MS Memory Limit: 10000K Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had

POJ1276Cash Machine[多重背包可行性]

Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 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