1 Star 0 Fork 1

saigon/Algorithms

forked from charlieshu/Algorithms 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
P2851 [USACO06DEC]The Fewest Coins G.cpp 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
charlie 提交于 2024-01-09 00:01 . move from github to gitee
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
const int inf=0x7fffffff;
struct coin{
int num,value,weight=1;
};
int main(){
int n,t,ans=inf;
scanf("%d%d",&n,&t);
coin a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i].value);
int sum=0;
for(int i=0;i<n;i++){
scanf("%d",&a[i].num);
sum += a[i].num*a[i].value;
}
int dp_b[sum-t+1],dp1_j[sum+1],dp2_j[sum+1];
for(int i=0;i<=sum-t;i++)
dp_b[i] = inf;
dp_b[0] = 0;
for(int i=0;i<=sum;i++){
dp1_j[i] = inf;
dp2_j[i] = inf;
}
dp1_j[0] = 0;
dp2_j[0] = 0;
//b
for(int i=1;i<=n;i++)
for(int j=a[i-1].value;j<=sum-t;j++)
dp_b[j] = min(dp_b[j],dp_b[j-a[i-1].value]==inf?inf:dp_b[j-a[i-1].value]+a[i-1].weight);
//j
for(int i=1;i<=n;i++){
for(int j=1;j<=sum;j++)
for(int k=0;k<=min(a[i-1].num,j/a[i-1].value);k++)
dp2_j[j] = min(dp2_j[j],dp1_j[j-k*a[i-1].value]==inf?inf:dp1_j[j-k*a[i-1].value]+k*a[i-1].weight);
memcpy(dp1_j,dp2_j,sizeof(dp1_j));
}
// for(int i=0;i<=sum;i++)
// cout<<(dp1_j[i]==inf?-1:dp1_j[i])<<" ";
// cout<<endl;
//
// for(int i=0;i<=sum-t;i++)
// cout<<(dp_b[i]==inf?-1:dp_b[i])<<" ";
// cout<<endl;
//
// cout<<dp1_j[75]<<" "<<dp_b[5]<<endl;
//cmp
for(int i=0;i<=sum-t;i++)
ans = min(ans,dp_b[i]+dp1_j[i+t]>=0?dp_b[i]+dp1_j[i+t]:inf);
printf("%d",(ans==inf?-1:ans));
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/saigonshu/algorithm.git
git@gitee.com:saigonshu/algorithm.git
saigonshu
algorithm
Algorithms
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385