首页 > 代码库 > ZJOI2006物流运输

ZJOI2006物流运输

唉,没想出来……

注意到预处理的作用。还有CLJ大牛说的话:这么小的数据,想干什么都可以。

SPFA预处理+DP 够经典

var
    f:array[0..100,0..100]of longint;
    a:array[0..20,0..100]of boolean;
    head,next,go,w,q:array[0..10000]of longint;
    dis:array[0..20]of longint;
    v,can:array[0..20]of boolean;
    n,m,k,e,tot,l,r,h,t:longint;

function min(x,y:longint):longint;
begin
    if x<y then exit(x);
    exit(y);
end;

procedure spfa;
var
    i,j,x:longint;
begin
    h:=0;
    t:=1;
    q[1]:=1;
    for i:=2 to m do
      dis[i]:=21470000;
    dis[1]:=0;
    for i:=2 to m do
      v[m]:=false;
    v[1]:=true;
    for i:=1 to m do
      begin
        can[i]:=true;
        for j:=l to r do
          if a[i,j] then can[i]:=false;
      end;
    while h<=t do
      begin
        inc(h);
        x:=q[h];
        v[x]:=false;
        i:=head[x];
        while i<>0 do
          begin
            j:=go[i];
            if can[j] then
            if dis[j]>dis[x]+w[i] then
            begin
              dis[j]:=dis[x]+w[i];
              if v[x]=false then
              begin
                inc(t);
                v[j]:=true;
                q[t]:=j;
              end;
            end;
            i:=next[i];
          end;
      end;
    f[l,r]:=dis[m]*(r-l+1);
end;

procedure insert(x,y,z:longint);
begin
    inc(tot);
    go[tot]:=y;
    next[tot]:=head[x];
    head[x]:=tot;
    w[tot]:=z;
end;

procedure init;
var
    i,j,x,y,z:longint;
begin
    read(n,m,k,e);
    for i:=1 to e do
      begin
        read(x,y,z);
        insert(x,y,z);
        insert(y,x,z);
      end;
    read(e);
    fillchar(a,sizeof(a),false);
    for i:=1 to e do
      begin
        read(x,y,z);
        for j:=y to z do
          a[x,j]:=true;
      end;
    for l:=1 to n do
      for r:=l to n do
        spfa;
end;
procedure dp;
var
    i,j,l:longint;
begin
    for i:=1 to n-1 do
      for j:=1 to n-i do
        for l:=j to i+j-1 do
          f[j,i+j]:=min(f[j,i+j],f[j,l]+k+f[l+1,i+j]);
    write(f[1,n]);
end;

begin
    init;
    dp;
end.