天天看點

poj 3260 The Fewest Coins 混合背包+上限處理 ★★★

The Fewest Coins

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5872 Accepted: 1763

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 CNcoins 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 1,  V 2, ...,  VN coins ( V 1, ... VN) 

Line 3: N space-separated integers, respectively  C 1,  C 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      

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.

Source

USACO 2006 December Gold

思路大家都清楚:總硬币數=出的數量  +找的數量

ans={dp[cost]+dp2[get] };

最坑爹的上限處理,你有沒想過,像這種題目,要麼就是不用考慮上限有多大(就在給出的T附近),要麼就是上限通過特定的方法

求出上限。

我剛開始認為上限就在T附近,比T多一點,而且多重背包還寫錯了,wa了不知道多少法。

後來看題解,發現上限是:

設ret=max{val[i]};

上限=ret*ret+T;

原因是:對于x>ret*ret 且x可達,     

在 [1,ret*ret]中至少存在一個y,使得 x=y(mod val[i]);  (i=1,2,3...,n);

(鴿籠原理)

實際上,我自己這個地方也沒搞懂,總覺得網上很多證明開頭有道理,後面就看不懂了。。

不過至少要把數組開大點。

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn= 100+10   ;
const int maxV= 120*120+10000+20   ;
//const int maxm=    ;

int n,des,V;
int val[maxn];
int num[maxn];
int dp[maxV];
int dp2[maxV];
int main()
{
    while(~scanf("%d%d",&n,&des))
    {
        int ret=0;
        FOR1(i,n)  {scanf("%d",&val[i]);ret=max(val[i],ret);}
        FOR1(i,n)  scanf("%d",&num[i]);
        V=ret*ret+des;
        memset(dp,0x3f,  (V+1)* sizeof dp[0]);
        dp[0]=0;
        for(int i=1;i<=n;i++)
        {
            int tot=num[i];
            int cnt;
            for( cnt=1; cnt<=tot ;tot-=cnt,cnt*=2 )
            {
                int tval=cnt*val[i];
                for(int v=V;v>=tval;v--)
                {
                    dp[v]=min(dp[v-tval]+cnt,dp[v] );
                }

            }
            if(!tot)  continue;
            int tval=tot*val[i];
             for(int v=V;v>=tval;v--)
             {
                    dp[v]=min(dp[v-tval]+tot,dp[v] );
             }
        }
        ret=ret*ret;
         memset(dp2,0x3f, ret* sizeof dp2[0]);
         dp2[0]=0;
         for(int i=1;i<=n;i++)
        {
            for(int v=val[i];v<=ret;v++)
            {
                dp2[v]=min(dp2[v-val[i]]+1,dp2[v]);
            }
        }

        int ans=INF;
        for(int v=des;v<=V;v++)
        {
            int x=v-des;
            ans=min(ans, dp2[x]+dp[v] );
        }
        if(ans==INF)  puts("-1");
        else printf("%d\n",ans);
    }

    return 0;
}
/*
3 140
10 20 50
5  5   5
3 140
10 20 50
5  5   1

3 225
1     50     74
1000   100   0

1   225
1
1
*/