超时怎么破

program jmcb (input,output);
type struct=record
p:array[0..101]of longint;
end;
var a:array[0..101]of struct;
var i,j,k,n,minh:longint;
sumh,sumt:array[0..101]of longint;
f:array[0..101,0..10001]of longint;
function pan:boolean;
var
i,j:longint;
begin
pan:=true;
for i:=1 to n do
for j:=1 to n do
if (f[i,minh]<>f[j,minh])or(f[i,minh]=0) then
exit(false);
end;
begin
assign(input,'input.txt');
assign(output,'output.txt');
reset(input);
rewrite(output);
readln(n);
minh:=maxlongint;
for i:=1 to n do
begin
j:=0;
while not eoln do
begin
j:=j+1;
read(a[i].p[j]);
if a[i].p[j]=-1 then
sumt[i]:=j-1;
if a[i].p[j]>=0 then
sumh[i]:= sumh[i]+a[i].p[j];
end;
if sumh[i]<minh then
minh:=sumh[i];
readln;
end;
fillchar(f,sizeof(f),0);
repeat
for k:=1 to n do
begin
for i:=1 to sumt[k] do
for j:=minh downto 1 do
if (j>=a[k].p[i])and(f[k,j]<f[k,j-a[k].p[i]]+a[k].p[i]) then
f[k,j]:=f[k,j-a[k].p[i]]+a[k].p[i];
if (k>1)and(f[k-1,minh]<>f[k,minh]) then
break;
end;
fillchar(f,sizeof(f),0);
minh:=minh-1;
until minh=0;
if minh=0 then
write(minh);
close(input);
close(output);
end.

0 条评论

目前还没有评论...

信息

ID
1059
难度
6
分类
动态规划 | 背包 点击显示
标签
(无)
递交数
7828
已通过
2342
通过率
30%
被复制
19
上传者