bzoj 1003: [ZJOI2006]物流运输 动态规划+最短路

来源:互联网 发布:汕头美工培训机构 编辑:程序博客网 时间:2024/05/10 22:10

分析:一开始没思路,看了题解后才明白。

cost[i,j]表示第i天到第j天用同一条路径的最短费用,这个可以预处理出来。

f[i]表示前i天的最小成本。

f[i]=min(f[j]+cost[j+1,i]+k)


代码:

var  a,cost,s:array[0..200,0..200] of longint;  n,m,k:longint;  f:array[0..200] of longint;  v:array[1..30] of boolean;  d,state:array[1..30] of longint;procedure init;var  e,x,y,z,d,i,j:longint;begin  readln(n,m,k,e);  for i:=1 to m do    for j:=1 to m do      a[i,j]:=maxlongint div 3;  for i:=1 to e do  begin    readln(x,y,z);    a[x,y]:=z;    a[y,x]:=z;  end;  readln(d);  for i:=1 to d do  begin    readln(z,x,y);    for j:=x to y do      s[z,j]:=1;  end;  for i:=1 to m do    for j:=1 to n do      s[i,j]:=s[i,j-1]+s[i,j];end;function spfa(x,y:longint):longint;var  i,u,head,tail:longint;begin  fillchar(v,sizeof(v),true);  v[1]:=false;  for i:=2 to m do    d[i]:=maxlongint div 3;  d[1]:=0;  head:=0;  tail:=1;  state[1]:=1;  repeat    inc(head);    if head>m+5 then head:=1;    u:=state[head];    for i:=1 to m do      if (s[i,y]-s[i,x-1]=0)and(d[u]+a[u,i]<d[i]) then      begin        d[i]:=d[u]+a[u,i];        if v[i] then        begin          v[i]:=false;          inc(tail);          if tail>m+5 then tail:=1;          state[tail]:=i;        end;      end;    v[u]:=true;  until head=tail;  spfa:=d[m];end;procedure main;var  i,j,w:longint;begin  for i:=1 to n do    for j:=i to n do    begin      w:=spfa(i,j);      if w<maxlongint div 3        then cost[i,j]:=w*(j-i+1)        else cost[i,j]:=w;    end;  for i:=1 to n do  begin    f[i]:=maxlongint div 3;    for j:=0 to i-1 do      if f[j]+cost[j+1,i]+k<f[i] then        f[i]:=f[j]+cost[j+1,i]+k;  end;  writeln(f[n]-k);end;begin  init;  main;end.


0 0
原创粉丝点击