Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:多重背包转化为01背包,我的63ms才过,换成完全背包再写下。。。

Posted by jayhaizeizai at 2011-08-17 14:41:03 on Problem 1276
In Reply To:多重背包转化为01背包,我的63ms才过,换成完全背包再写下。。。 Posted by:lijingwei at 2011-08-17 14:20:33
我写的多重+优化 16ms
贴下代码
顺便问下LZ的做法 貌似跟我的不同


Program P1276;
 const
      maxc=100000;
      maxn=10;
 var
      f:array[-maxc..maxc]of boolean;
	  v:array[-maxc..maxc]of longint;
      sum,w:array[1..maxn]of longint;
      cash,n:longint;
 procedure init;
 var i:longint;
  begin
     fillchar(sum,sizeof(sum),0);
     fillchar(w,sizeof(w),0);
     read(cash);
	 read(n);
	 for i:=1 to n do read(sum[i],w[i]);
	 readln;
	 fillchar(f,sizeof(f),false);
	 fillchar(v,sizeof(v),0);
  end;

 procedure main;
 var i,j,k,cmax,tmp:longint;
  begin
   cmax:=0;
   f[0]:=true;
   for i:=1 to n do
   begin
    for j:=cmax downto 0 do
	 if f[j] then
	  for k:=1 to sum[i] do
       begin
	    tmp:=j+w[i]*k;
		if tmp>cash then break;
		if (v[tmp]<>0)and(v[tmp]<v[j]+1) then break;
		v[tmp]:=v[j]+1;
		f[tmp]:=true;
		if tmp>cmax then cmax:=tmp;
	  end;
   end;
   writeln(cmax);
   end;



begin
  assign(input,'P1276.in');
  assign(output,'P1276.out');
  reset(input);
  rewrite(output);
  while not eof do
   begin
    init;
    main;
   end;
  close(input);
  close(output);
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator