程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1742

POJ 1742

編輯:C++入門知識

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 V1, V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1, C2 coins of value V2, ...., 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 V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., 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

Hint

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.

下面是我自己寫的代碼從狀態0出發看能到達哪些狀態,最後把所有的狀態種數輸出就行

#include 
#include
#include
#include
#define M 100001
int dp[M];
inline int max(int a,int b)
{
    return a>b?a:b;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m)&&(m+n))
    {
        int a[101],c[101];
        int f=0;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)scanf("%d",a+i);
        for(int i=1;i<=n;i++)scanf("%d",c+i);
        dp[0]=1;
        for(int i=1;i<=n;i++)
        {
            if(a[i]*c[i]>=m)
            {
                for(int j=a[i];j<=m;j++)
                if(!dp[j]&&dp[j-a[i]])dp[j]=1;
            }
            else
            {
                int k=1;
                while(k=k*a[i];j--)
                        if(!dp[j]&&dp[j-k*a[i]])dp[j]=1;
                    c[i]-=k;
                    k<<=1;
                }
                for(int j=m;j>=c[i]*a[i];j--)
                    if(!dp[j]&&dp[j-c[i]*a[i]])dp[j]=1;
            }
        }
        for(int j=1;j<=m;j++)
            if(dp[j])f++;
        printf("%d\n",f);
    }
    return 0;
}

下面是某大牛寫的,把多重背包轉化成完全背包,時間就優化了不少。

#include
#include
#include
using namespace std;
const int MAX = 1e5+10;
int use[MAX]; int dp[MAX];
int a[MAX],b[MAX];
int main()
{

    int n,m,ans;
    while(scanf("%d %d",&n,&m)==2&&(n||m))
    {
        for(int i=0;i


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved