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. Installing Apache Spark on Ubuntu 16.04

    Santosh Srinivas on 07 Nov 2016, tagged onApache Spark, Analytics, Data Minin I've finally got to a ...

  2. 特殊汉字“𣸭”引发的对于字符集的思考;mysql字符集;sqlalchemy字符集设置;客户端字符集设置;

    字符集.字符序的概念与联系 在数据的存储上,MySQL提供了不同的字符集支持.而在数据的对比操作上,则提供了不同的字符序支持. MySQL提供了不同级别的设置,包括server级.database级. ...

  3. 【BZOJ】【4066】简单题(强制在线)

    KD-Tree KD-Tree的进阶姿势戳这里 http://zyfzyf.is-programmer.com/posts/92431.html 为啥有种线段树&平衡树的即视感……(树形结构的 ...

  4. 3D屏保:排色榜

    3D屏保:排色榜 排色榜,是一个针对图形学中的色彩进行排序的DEMO,这里的色是色彩的意思,看成别的点进来的同学请自觉面壁.该DEMO可以按RGB,GBR,BRG,灰度值四种方式进行排序.排序算法为冒 ...

  5. [每日一题] OCP1z0-047 :2013-08-04 INSERT --- WITH CHECK OPTION

    这题是考:insertWITH CHECK OPTION的用法 insert into (<select clause> WITH CHECKOPTION) values (...) 这样 ...

  6. Android GUI之View布局

    在清楚了View绘制机制中的第一步测量之后,我们继续来了解分析View绘制的第二个过程,那就是布局定位.继续跟踪分析源码,根据之前的流程分析我们知道View的绘制是从RootViewImpl的perf ...

  7. 如何在Linux中发现IP地址冲突

    导读 你们都知道什么是IP地址,是吧?它们被分配给网络上的设备来代表它们.它们通过DHCP服务器分配并且会经常改变.现在有两种IP地址.动态的一种会经常改变(几天一次),而静态的就如它的名字那样是静态 ...

  8. Sql server management studio: cannot find one or more components

      Install VS2010 SHELL 独立组件 https://www.microsoft.com/en-US/download/details.aspx?id=1366 运行安装程序,rep ...

  9. mobile移动网页开发常用代码模板

    index.html <!DOCTYPE HTML> <html> <head> <!--申明当前页面的编码集--> <meta http-equ ...

  10. wireshark 的使用(filter的用法)

    转自:http://blog.csdn.net/hanyuxinting/article/details/5558095 过滤器语法---------------------------------- ...