The trouble of Xiaoqian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1076    Accepted Submission(s): 355

Problem Description
In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed to her.)
And now , Xiaoqian 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). Xiaoqian 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 .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.
 
Input
There are several test cases in the 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
The end of the input is a double 0.
 
Output
Output one line for each test case like this ”Case X: Y” : X presents the Xth test case and Y presents the minimum number of coins . If it is impossible to pay and receive exact change, output -1.
 
Sample Input
3 70
5 25 50
5 2 1
0 0
 
Sample Output
Case 1: 3

多重背包.

代码:

     #include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f;
struct node
{
int v,c;
};
node sta[];
int dp[];
int dp2[];
int main()
{
int n,t,i,j,maxc,cnt=; //开始cnt赋值在while里面,娘希匹,错了10+
while(scanf("%d%d",&n,&t),n+t)
{
maxc=-inf; for(i=;i<n;i++)
{
scanf("%d",&sta[i].v);
if(maxc<sta[i].v) maxc=sta[i].v;
}
for(i=;i<n;i++)
scanf("%d",&sta[i].c);
maxc+=t;
for(i=;i<=maxc+;i++)
dp[i]=inf;
dp[]=;
for(i=;i<n;i++)
{
if(sta[i].v*sta[i].c>=t) /*完全背包*/
{
for(j=sta[i].v ; j<=maxc ;j++)
{
if(dp[j]>dp[j-sta[i].v]+)
dp[j]=dp[j-sta[i].v]+;
}
}
else
{
int k=;
while(sta[i].c>k)
{
for( j=maxc ; j>=sta[i].v*k ; j-- )
{
if(dp[j]>dp[j-sta[i].v*k]+k)
dp[j]=dp[j-sta[i].v*k]+k;
}
sta[i].c-=k;
k<<=;
}
for( j=maxc; j>=sta[i].c*sta[i].v ; j-- )
{
if(dp[j]>dp[j-sta[i].v*sta[i].c]+sta[i].c)
dp[j]=dp[j-sta[i].v*sta[i].c]+sta[i].c;
}
} }
for(i=;i<=maxc+;i++)
dp2[i]=inf;
dp2[]=;
for(i=;i<n;i++)
{
for(j=sta[i].v ;j<=maxc;j++)
{
if(dp2[j]>dp2[j-sta[i].v]+)
dp2[j]=dp2[j-sta[i].v]+ ;
}
}
int ans=inf;
for(i=t;i<=maxc ;i++)
{
if(ans>dp[i]+dp2[i-t]) ans=dp[i]+dp2[i-t];
}
if(ans==inf) printf("Case %d: -1\n",cnt++);
else
printf("Case %d: %d\n",cnt++,ans); }
return ;
}

第二种...

 #include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f;
struct node
{
int v,c;
};
node sta[];
int dp[];
int dp2[];
int main()
{
int n,t,i,j,maxc,cnt=;
while(scanf("%d%d",&n,&t),n+t)
{
maxc=-inf;
for(i=;i<n;i++)
{
scanf("%d",&sta[i].v);
if(maxc<sta[i].v) maxc=sta[i].v;
}
for(i=;i<n;i++)
scanf("%d",&sta[i].c);
maxc+=t;
memset(dp,-,sizeof(dp[])*(maxc+));
dp[]=;
for(i=;i<n;i++)
{
if(sta[i].v*sta[i].c>=t) /*完全背包*/
{
for(j=sta[i].v ; j<=maxc ;j++)
{
if(dp[j-sta[i].v]!=-&&(dp[j]==-||dp[j]>dp[j-sta[i].v]+))
dp[j]=dp[j-sta[i].v]+;
}
}
else
{
int k=;
while(sta[i].c>k)
{
for( j=maxc ; j>=sta[i].v*k ; j-- )
{
if(dp[j-sta[i].v*k]!=-&&(dp[j]==-||dp[j]>dp[j-sta[i].v*k]+k))
dp[j]=dp[j-sta[i].v*k]+k;
}
sta[i].c-=k;
k<<=;
}
for( j=maxc; j>=sta[i].c*sta[i].v ; j-- )
{
if(dp[j-sta[i].v*sta[i].c]!=-&&(dp[j]==-||dp[j]>dp[j-sta[i].v*sta[i].c]+sta[i].c))
dp[j]=dp[j-sta[i].v*sta[i].c]+sta[i].c;
}
} }
memset(dp2,-,sizeof(dp2[])*(maxc+));
dp2[]=;
for(i=;i<n;i++)
{
for(j=sta[i].v ;j<=maxc;j++)
{
if(dp2[j-sta[i].v]!=-&&(dp2[j]==-||dp2[j]<dp2[j-sta[i].v]+))
dp2[j]=dp2[j-sta[i].v]+ ;
}
}
int ans=inf;
for(i=t;i<=maxc ;i++)
{
if(dp2[i-t]!=-&&dp[i]!=-&&ans>dp[i]+dp2[i-t])
ans=dp[i]+dp2[i-t];
}
if(ans==inf) printf("Case %d: -1\n",cnt++);
else
{
printf("Case %d: %d\n",cnt++,ans);
} }
return ;
}

HDUOJ-----3591The trouble of Xiaoqian的更多相关文章

  1. The trouble of Xiaoqian

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  2. hdu 3591 The trouble of Xiaoqian

    hdu 3591  The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西 ...

  3. HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

    HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...

  4. HDU 3594 The trouble of Xiaoqian 混合背包问题

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  5. hdu3591The trouble of Xiaoqian 多重背包+全然背包

    //给出Xiaoqian的钱币的价值和其身上有的每种钱的个数 //商家的每种钱的个数是无穷,xiaoqian一次最多付20000 //问如何付钱交易中钱币的个数最少 //Xiaoqian是多重背包 / ...

  6. HDU - 3591 The trouble of Xiaoqian 题解

    题目大意 有 \(N\) 种不同面值的硬币,分别给出每种硬币的面值 \(v_i\) 和数量 \(c_i\).同时,售货员每种硬币数量都是无限的,用来找零. 要买价格为 \(T\) 的商品,求在交易中最 ...

  7. hdu 3591 多重加完全DP

    题目: The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  8. HDU 3591 (完全背包+二进制优化的多重背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...

  9. HDU_3591_(多重背包+完全背包)

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

随机推荐

  1. Android图片加载框架最全解析(三),深入探究Glide的缓存机制

    在本系列的上一篇文章中,我带着大家一起阅读了一遍Glide的源码,初步了解了这个强大的图片加载框架的基本执行流程. 不过,上一篇文章只能说是比较粗略地阅读了Glide整个执行流程方面的源码,搞明白了G ...

  2. (转)不通过web.config在运行时注册httpmodules

    https://blog.csdn.net/kufeiyun/article/details/7763070 在asp.net4 中,我们知道可以不用任何配置让一个方法在appdomain中尽早执行, ...

  3. 从win10家庭版/中文版升级到win10专业版

    发布时间:2015-8-4     很多同学在不了解win10系统版本的情况下,安装了win10家庭版/中文版,不管是win10家庭版,还是win10家庭中文版,或者是win10家庭单语言版,它们都是 ...

  4. iOS:UIToolBar、toolbarItems、BarButtonItem的几种关系

    工具栏:ToolBar 工具栏项目:Bar Button Item 调节按钮位置的固定调节:Fixed Space Bar Button Item 调节按钮位置的灵活调节:Flexible Space ...

  5. JQuery缓冲加载图片插件lazyload.js的使用方法

    lazyload.js是一个基于JQuery的插件,可以用来缓冲加载图片.如果一个网页很长并且有很多图片的话,下载图片就需要很多时间,那么就会影响整个网页的加载速度,而这款延迟加载插件,会通过你的滚动 ...

  6. Vue路由history模式踩坑记录:nginx配置解决404问题

    问题背景: vue-router 默认是hash模式,使用url的hash来模拟一个完整的url,当url改变的时候,页面不会重新加载.但是如果我们不想hash这种以#号结尾的路径时候的话,我们可以使 ...

  7. Android界面设计之对话框——定制Toast、AlertDialog

    一.概述 在界面设计中需要根据用户操作显示提示信息.出错信息等,就要用到对话框.Android实现提示信息显示常用有两种方式 1.Toast 2.AlertDialog 二.Toast Android ...

  8. Jmeter-Maven-Plugin高级应用:Remote Server Configuration

    Remote Server Configuration Pages 12 Home Adding additional libraries to the classpath Advanced Conf ...

  9. ZH奶酪:PHP判断图片格式的7种方法

    以图片 $imgurl = "http://www.php10086.com/wp-content/themes/inove/img/readers.gif"; 为例: 思路1. ...

  10. Java从零开始学二(标识符和关键字)

    标识符.关键字.注释 一.标识符 Java中的包.类.方法.参数和变量的名字由任意顺序的大小字母.数字.下划线(_).和美元符号($)组成, 标识符:不能以数字开头.也不能是JAVA中的保留关键字 如 ...