【DP】 POJ 3260 The Fewest Coins 背包
来源:互联网 发布:东方财富 for mac 编辑:程序博客网 时间:2024/05/21 07:13
购买T 块钱的东西 交易过程中使用最少的硬币数量
因为客人的硬币有限,商店的硬币无限
先多重背包求出给商店的硬币数
完全背包求出找回的硬币数
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <string>#include <iostream>#include <algorithm>#include <sstream>#include <math.h>using namespace std;#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>#define cler(arr, val) memset(arr, val, sizeof(arr))#define IN freopen ("in.txt" , "r" , stdin);#define OUT freopen ("out.txt" , "w" , stdout);typedef long long LL;const int MAXN = 1000006;//点数的最大值const int MAXM = 20006;//边数的最大值const int INF = 11521204;const int mod=1000000007;int a[10000],w[10000],p[10000],dp[313232],f[323131],c[11111];int main(){ int n,t; // IN; while(scanf("%d%d",&n,&t)!=EOF) { int tol=0,maxn=0; for(int i=1;i<=n;i++) scanf("%d",&a[i]),maxn=max(maxn,a[i]); for(int i=1;i<=n;i++) scanf("%d",&c[i]); maxn=maxn*maxn+t+1; fill(dp,dp+maxn+1,INF); dp[0]=0; for(int i=1;i<=n;i++) { for(int j=1;j<=c[i];j*=2) { w[tol]=j*a[i]; p[tol++]=j; c[i]-=j; } if(c[i]) w[tol]=c[i]*a[i],p[tol++]=c[i]; } for(int i=0;i<tol;i++) { for(int j=maxn;j>=w[i];j--) dp[j]=min(dp[j],dp[j-w[i]]+p[i]); } fill(f,f+maxn+1,INF); f[0]=0; for(int i=0;i<n;i++) { for(int j=a[i];j<=maxn;j++) f[j]=min(f[j],f[j-a[i]]+1); } int ans=INF; for(int i=0;i<=maxn-t;i++) ans=min(ans,dp[i+t]+f[i]); if(ans==INF) ans=-1; printf("%d\n",ans); } return 0;}
0 0
- 【DP】 POJ 3260 The Fewest Coins 背包
- 【DP|混合背包】POJ-3260 The Fewest Coins
- poj 3260 The Fewest Coins(背包)
- POJ 3260 The Fewest Coins / 混合背包
- poj 3260 The Fewest Coins(背包)
- POJ - 3260 The Fewest Coins (完全背包)
- POJ 3260The Fewest Coins (DP)
- poj 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins 多重背包+完全背包
- poj 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins (多重背包 + 完全背包)
- poj 3260 The Fewest Coins (多重背包 + 完全背包)
- POJ 3260 - The Fewest Coins(完全背包+多重背包)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- poj 3260 the fewest coins
- POJ-3260-The Fewest Coins
- 黑马程序员_iso入门基础学习_C语言day6
- 输入板火爆App Store 搜狗无线战略渐清晰
- oracle数据库密码过期的解决方案
- 【算法】KMP的理解
- 使用Linq求和方法Sum计算集合中多个元素和时应该注意的性能问题
- 【DP】 POJ 3260 The Fewest Coins 背包
- 程序调试过程中输出Log
- QML类型说明-GaussianBlur
- HMM(隐马尔科夫模型)
- 输入板火爆App Store 搜狗无线战略渐清晰
- Android调用WCFRest
- [Codeforces]Round246 div2 解题报告
- poj 3259 Wormholes
- 几种电子纸显示技术对比