Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:为什么在I和J间要建在(I+J)/2这个点?我用的是(dist[i]+dist[j])/2,再找和这个中点最近的饭店为什么就wa?In Reply To:为什么在I和J间要建在(I+J)/2这个点?我用的是(dist[i]+dist[j])/2,再找和这个中点最近的饭店为什么就wa? Posted by:The_Dawn at 2011-08-07 22:31:08 把中间注释掉的部分用tmp:=(i+j)div 2换掉就对了。 求原因!! var n,m,i,j,k,maxn,tmp,mid,minn,ans,chain:longint; d:array[0..201]of longint; f:array[0..201,0..31]of longint; link,cost,depot:array[0..201,0..201]of longint; procedure renew_init; begin ans:=0; inc(chain); fillchar(link,sizeof(link),0); fillchar(f,sizeof(f),0); fillchar(cost,sizeof(cost),0); fillchar(depot,sizeof(depot),0); read(n,m); if n+m=0 then halt; for i:=1 to n do read(d[i]); end; procedure prework; begin for i:=1 to n-1 do for j:=i+1 to n do begin {minn:=maxlongint; mid:=(d[i]+d[j])div 2; for k:=i to j do if abs(d[k]-mid)<minn then begin minn:=abs(d[k]-mid); tmp:=k; end;} tmp:=(i+j)div 2; depot[i,j]:=tmp; for k:=i to j do inc(cost[i,j],abs(d[k]-d[tmp])); end; for i:=1 to n do depot[i,i]:=i; end; procedure dawn; begin for i:=0 to n do for j:=0 to m do f[i,j]:=maxlongint div 10; f[0,0]:=0; for i:=1 to n do for j:=1 to m do if j<=i then begin minn:=maxlongint; for k:=j-1 to i-1 do if f[k,j-1]+cost[k+1,i]<minn then begin minn:=f[k,j-1]+cost[k+1,i]; link[i,j]:=k; end; f[i,j]:=minn; end; end; procedure print(n,m:longint); begin if n=0 then exit; print(link[n,m],m-1); if link[n,m]=n-1 then writeln('Depot ',m,' at restaurant ',depot[link[n,m]+1,n],' serves restaurant ',n) else writeln('Depot ',m,' at restaurant ',depot[link[n,m]+1,n],' serves restaurants ',link[n,m]+1,' to ',n); inc(ans,cost[link[n,m]+1,n]); end; begin repeat renew_init; writeln('Chain ',chain); prework; dawn; print(n,m); writeln('Total distance sum = ',ans); writeln; until false; end. Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator