poj 3260 The Fewest Coins(背包)

http://poj.org/problem?id=3260

The Fewest Coins
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4736   Accepted: 1422

Description

Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1C2 coins of valueV2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

Input

Line 1: Two space-separated integers:  N and  T
Line 2: N space-separated integers, respectively  V 1V 2, ...,  VN coins ( V 1, ... VN
Line 3: N space-separated integers, respectively  C 1C 2, ...,  CN

Output

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

Sample Input

3 70
5 25 50
5 2 1

Sample Output

3
题意:有N种硬币,告诉你N种硬币的价值,以及每种硬币你拥有的数目,现在你要付费T元,而卖家对于每种硬币都有无数个,问流通的硬币(你付给卖家的,与卖家找给你的)最少为多少个?
假设你付给卖家x元(x>=T),卖家找你x-T元。我们可以用多重背包,算出你付x元所需的最少硬币数,用完全背包算出卖家找x-T所需的最少硬币数,我们再枚举x就能求得答案。但是x还需要一个枚举的上界。我定的上界为20000,单纯凭感觉。。。。具体上界的确定方法我也不太清楚。。代码如下:
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#define nn 110
#define inff 0x3fffffff
#define mod 1000000007
#define eps 1e-9
using namespace std;
typedef long long LL;
int n,t;
int v[nn],c[nn];
int f[2*nn*nn],dp[110][2*nn*nn];
vector<int>ve[150];
int po[25];
int main()
{
    int i,j,k;
    po[0]=1;
    for(i=1;i<=20;i++)
        po[i]=po[i-1]*2;
    while(scanf("%d%d",&n,&t)!=EOF)
    {
        for(i=1;i<=n;i++)
            scanf("%d",&v[i]);
        for(i=1;i<=n;i++)
            ve[i].clear();
        int ix;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&c[i]);
            ix=0;
            for(j=0;j<=20;j++)
            {
                if(ix+po[j]>=c[i])
                {
                    if(c[i]-ix>0)
                        ve[i].push_back(c[i]-ix);
                    break;
                }
                ve[i].push_back(po[j]);
                ix+=po[j];
            }
        }
        for(i=1;i<=10000*2;i++)
            f[i]=inff;
        f[0]=0;
        for(i=1;i<=n;i++)
        {
            for(j=v[i];j<=10000*2;j++)
            {
                f[j]=min(f[j],f[j-v[i]]+1);
            }
        }
        for(i=0;i<=2*10000;i++)
        {
            for(j=0;j<=n;j++)
                dp[j][i]=inff;
        }
        dp[0][0]=0;
        for(i=1;i<=n;i++)
        {
            for(k=2*10000;k>=0;k--)
                dp[i][k]=min(dp[i][k],dp[i-1][k]);
            for(j=0;j<(int)ve[i].size();j++)
            {
                for(k=2*10000;k>=0;k--)
                {
                    if(k-v[i]*ve[i][j]>=0)
                    {
                        dp[i][k]=min(dp[i][k],dp[i-1][k-v[i]*ve[i][j]]+ve[i][j]);
                        dp[i][k]=min(dp[i][k],dp[i][k-v[i]*ve[i][j]]+ve[i][j]);
                    }
                }
            }
        }
        int ans=inff;
        for(i=t;i<=2*10000;i++)
        {
            ans=min(ans,dp[n][i]+f[i-t]);
        }
        if(ans==inff)
            puts("-1");
        else
            printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(dp,动态规划,ACM,背包)