/ Vijos / 讨论 / 采药 /

为什么这种写法(DP)只能过2个点..

#include <iostream>
#include <cstdlib>
#include <stdio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
long maxt,m,i,j;
long t[101],p[101];
long opt[200]={0};
using namespace std;
int main(int argc, char *argv[]) {
cin>>maxt;
cin>>m;
for(i=1;i<=m;i++){
cin>>t[i];
cin>>p[i];
}
for(i=1;i<=m;i++){
for(j=maxt;j>=t[i];j--){
if(opt[j-t[i]]+p[i]>opt[j]){
opt[j]=opt[j-t[i]]+p[i];
}
}
}
cout<<opt[maxt]<<endl;
}

2 条评论

  • @ 2013-09-09 22:10:02

    给你个装箱问题的代码,回去看看背包9讲
    program p1133;
    var v,num,i,j:longint;
    c,f:array[1..20000]of longint;
    function max(a,b:longint):longint;
    begin
    if a>=b then
    exit(a);
    exit(b);
    end;
    begin
    readln(v);
    readln(num);
    for i:=1 to num do
    readln(c[i]);
    for i:=1 to num do
    for j:=v downto c[i] do
    if j>=c[i] then
    f[j]:=max(f[j],f[j-c[i]]+c[i]);
    writeln(v-f[v]);
    end.

  • @ 2013-08-31 14:29:16

    呵呵,因为你写得不对

  • 1

信息

ID
1104
难度
4
分类
动态规划 | 背包 点击显示
标签
递交数
16820
已通过
6526
通过率
39%
被复制
38
上传者