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. dom4j怎么获得指定名称的节点信息

    <?xml version="1.0" encoding="utf-8" ?> <MgUtil> <db_config> & ...

  2. Android之与当前连接的wifi进行文件夹的浏览与传输

    先上传jar文件:ConnectWifi.jar.zip 上传源文件:org.zip 使用实例及相应的注释: import java.io.File; import java.net.InetAddr ...

  3. C++语言笔记系列之十六——赋值兼容规则&amp;多继承的二义性

    1.赋值兼容规则 (1)派生类对象能够给基类对象赋值,这样的情况下派生类对象将从基类继承的成员的值赋值给一个基类对象:可是不同意将一个基类的对象赋值给一个派生类. (2)能够将派生类对象的地址赋给基类 ...

  4. (转)SQL Server 列转行

    原文:http://www.myexception.cn/sql-server/1078985.html1,2,3,4,5以上是一个字符串或则一逗号分隔的数字. 这里希望用一条语句查询出这样的效果: ...

  5. javascript和python取dict字典对象的不同

    dict1={"a":1,"b":2,"22a":44} JS: dict1.a 和 dict1["a"]都可以 pyt ...

  6. 【BZOJ】【2946】【POI2000】公共串

    后缀数组 好感动,复习了下后缀数组居然写出来了……(感谢ykz大神) 求最长公共子串……WA了一发是因为:[不同字符串之间要用不同的特殊字符隔开]否则就会匹配到相同→_→比如都是aaa结尾,如果用相同 ...

  7. 《C和指针》整理一

    1.C语言的凝视     在C语言中,假设须要凝视掉一段代码.且代码中可能会已经存在/**/凝视形式,那么能够使用: #if 0     statements #endif     这样的形式来凝视掉 ...

  8. Objective-c:NSFileHandle类,创建流对象,对文件进行写入、读取的操作

    NSFileHandle类:它需要配合NSFileManager文件管理类,对文件内容进行操作,写入数据.读取数据. 使用步骤:     1.打开文件获取NSFileHandle类的对象     2. ...

  9. Linux Shell处理文本最常用的工具大盘点

    导读 本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实 ...

  10. 1423 Greatest Common Increasing Subsequence (LCIS)

    讲解摘自百度; 最长公共上升子序列(LCIS)的O(n^2)算法? 预备知识:动态规划的基本思想,LCS,LIS.? 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列).? 首先我们可 ...