HDUOJ-----3591The trouble of Xiaoqian
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
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.
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.
5 25 50
5 2 1
0 0
多重背包.
代码:
#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的更多相关文章
- The trouble of Xiaoqian
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 3591 The trouble of Xiaoqian
hdu 3591 The trouble of Xiaoqian 题意:xiaoqi要买一个T元的东西,当前的货币有N种,xiaoqi对于每种货币有Ci个:题中定义了最小数量即xiaoqi拿去买东西 ...
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)
HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...
- HDU 3594 The trouble of Xiaoqian 混合背包问题
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu3591The trouble of Xiaoqian 多重背包+全然背包
//给出Xiaoqian的钱币的价值和其身上有的每种钱的个数 //商家的每种钱的个数是无穷,xiaoqian一次最多付20000 //问如何付钱交易中钱币的个数最少 //Xiaoqian是多重背包 / ...
- HDU - 3591 The trouble of Xiaoqian 题解
题目大意 有 \(N\) 种不同面值的硬币,分别给出每种硬币的面值 \(v_i\) 和数量 \(c_i\).同时,售货员每种硬币数量都是无限的,用来找零. 要买价格为 \(T\) 的商品,求在交易中最 ...
- hdu 3591 多重加完全DP
题目: The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 3591 (完全背包+二进制优化的多重背包)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...
- HDU_3591_(多重背包+完全背包)
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
随机推荐
- jsonConfig使用方法
1.先编写jsonConfig的初始化代码 private JsonConfig jsonConfig; public action构造方法() { jsonConfig = new JsonConf ...
- dlib landmark+人面识别
#include "stdafx.h" #include <dlib/image_processing/frontal_face_detector.h> #includ ...
- [转]C++之运算符重载(2)
上一节主要讲解了C++里运算符重载函数,在看了单目运算符(++)重载的示例后,也许有些朋友会问这样的问题.++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又 ...
- sqlalchemy简单示例
1.初始化数据库database.py #!/usr/bin/env python # -*- coding: utf-8 -*- # Created by miaoshuijian on 2017/ ...
- 【BZOJ】【4052】【CERC2013】Magical GCD
DP/GCD 然而蒟蒻并不会做…… Orz @lct1999神犇 首先我们肯定是要枚举下端点的……嗯就枚举右端点吧…… 那么对于不同的GCD,对应的左端点最多有log(a[i])个:因为每次gcd缩小 ...
- Leaflet绘制热力图【转】
http://blog.csdn.net/giser_whu/article/details/51485871 时下用的最多的开源二维webgis引擎应该是openlayers与leaflet了,le ...
- js 获取读取cookie
// --- 设置cookie function setCookie(sName, sValue, expireHours) { var cookieString = sName + &quo ...
- Design Your Own Protocol In Five Minutes
https://mayaposch.wordpress.com/2011/10/03/design-your-own-protocol-in-five-minutes ---------------- ...
- simple-libfm-example-part1
原文:https://thierrysilbermann.wordpress.com/2015/02/11/simple-libfm-example-part1/ I often get email ...
- DEDE标签综合
dede: sql标签: sql标签可以称得上是个万能标签了,查询数据库将其输出,这里介绍一些关于这个标签的用法: 1.用来输出统计内容,这个是不错的,举个例子,我们来统计下总共发了多少的文章,思路就 ...