羞耻,分组赛上考的,竟然没想出来,
对坐标离散化后区间dp即可
1 const inf=100000007; 2 var f:array[0..610,0..610] of longint; 3 v:array[0..10010] of longint; 4 a,b,h:array[0..610] of longint; 5 t,i,j,l,k,w,p,n,mx:longint; 6 7 function min(a,b:longint):longint; 8 begin 9 if a>b then exit(b) else exit(a); 10 end; 11 12 begin 13 readln(t); 14 while t>0 do 15 begin 16 dec(t); 17 readln(n); 18 mx:=0; 19 fillchar(v,sizeof(v),0); 20 for i:=1 to n do 21 begin 22 readln(a[i],b[i],h[i]); 23 v[a[i]]:=1; 24 v[b[i]]:=1; 25 if b[i]>mx then mx:=b[i]; 26 end; 27 p:=1; 28 for i:=1 to mx do 29 if v[i]=1 then 30 begin 31 inc(p); 32 v[i]:=p; 33 end; 34 35 for i:=1 to n do 36 begin 37 a[i]:=v[a[i]]; 38 b[i]:=v[b[i]]; 39 end; 40 h[0]:=-inf; 41 inc(p); 42 for l:=2 to p do 43 for i:=1 to p-l+1 do 44 begin 45 j:=i+l-1; 46 w:=0; 47 for k:=1 to n do 48 if (a[k]>i) and (b[k]<j) and (h[k]>h[w]) then w:=k; 49 f[i,j]:=inf; 50 if w=0 then f[i,j]:=0 51 else 52 for k:=a[w] to b[w] do 53 f[i,j]:=min(f[i,j],h[w]+f[i,k]+f[k,j]); 54 end; 55 writeln(f[1,p]); 56 end; 57 end.
时间: 2024-11-05 22:33:04