摘要:hdu1171: http://acm.hdu.edu.cn/showproblem.php?pid=1171题意:给定设备种类n,接下来输出n行,v[i]、m[i]表示有m[i]个价值为v[i]的设备,求将这些设备分为两部分,每部分的价值,要求两部分的价值相差最小。解法:混合背包,背包最大容量为总价值sum的一半s,要求价值尽量大,则f[s]和sum-f[s]即为答案。 code:#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int v[100],m[100
阅读全文
摘要:hdu2191: http://acm.hdu.edu.cn/showproblem.php?pid=2191题意:容量为v的背包,有n种物品,每种物品时有限个的,有不同体积及价值,求最大价值解法:多重背包code:#include<iostream>#include<cstdio>#include<algorithm>int v[200],w[200],c[200],f[200];int max(int a,int b){ if(a>b) return a; else return b;}int main(){ int t,n,m,i,j,k,x..
阅读全文
摘要:hdu1712: http://acm.hdu.edu.cn/showproblem.php?pid=1712题意:输入课程数n和总天数m,再输入矩阵n*m,v[i][j]表示花费j天在课程i上得到价值为v[i][j]解法:分组背包问题:组数为n,总容量为m,每件物品费用为c[i]=j,价值为w[i]=v[i][j]code:#include<iostream>#include<cstdio>#include<cstdlib>int max(int a,int b){ if(a>b) return a; else return b;}int f[120
阅读全文
摘要:hdu2602: http://acm.hdu.edu.cn/showproblem.php?pid=2602题意:有n个物品和容量为v的背包,每种物品只可取一个,且有不同价值及体积,求最大价值 解法:01背包code:#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int max(int a,int b){ if(a>b) return a; else return b;}int v[1010],w[1010],f[1010];int main(){ .
阅读全文
摘要:hdu2546: http://acm.hdu.edu.cn/showproblem.php?pid=2546题意:输入n,表示有n种菜可购买,再输入n个数v[i],表示菜的价格,再输入m,表示卡上有m元,规定若卡上余额大于等于5则可购买任意价钱的物品(即使买后余额为负),否则不可购买任何物品,求最后卡上最小余额解法:01背包+贪心:最贵的物品肯定要被购买到,所以先选出来最后购买。先对除了最贵的物品外其余物品进行01背包处理,总容量为m-5,物品费用和价值都为w[i]。code:#include<iostream>#include<cstdio>#include<
阅读全文