【Description】
物流公司要把一批货物从码头A运到码头B。由于货物量比较大,需要n天才能运完。货物运输过程中一般要转停好几个码头。物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪。由于各种因素的存在,有的时候某个码头会无法装卸货物。这时候就必须修改运输路线,让货物能够按时到达目的地。但是修改路线是一件十分麻烦的事情,会带来额外的成本。因此物流公司希望能够订一个n天的运输计划,使得总成本尽可能地小。
【Input】
第一行是四个整数n(1<=n<=100)、m(1<=m<=20)、K和e。n表示货物运输所需天数,m表示码头总数,K表示每次修改运输路线所需成本。接下来e行每行是一条航线描述,包括了三个整数,依次表示航线连接的两个码头编号以及航线长度(>0)。其中码头A编号为1,码头B编号为m。单位长度的运输费用为1。航线是双向的。再接下来一行是一个整数d,后面的d行每行是三个整数P( 1 < P < m)、a、b(1 < = a < = b < = n)。表示编号为P的码头从第a天到第b天无法装卸货物(含头尾)。同一个码头有可能在多个时间段内不可用。但任何时间都存在至少一条从码头A到码头B的运输路线。
【Output】
包括了一个整数表示最小的总成本。总成本=n天运输路线长度之和+K*改变运输路线的次数。
【Sample Input】
5 5 10 8
1 2 1
1 3 3
1 4 2
2 3 2
2 4 4
3 4 1
3 5 2
4 5 2
4
2 2 3
3 1 1
3 3 3
4 4 5
【Sample Output】
32
【Solution】
最短路+DP。考虑
t[i][j]
表示从第i天到第j天从A地到B地的最短路,那么前i天总费用最小值为
f[i]=min{f[j]+k+t[j+1][i]∗(i−j)}
。
代码如下:
/**************************************************************
Problem: 1003
User: llgyc
Language: Pascal
Result: Accepted
Time:136 ms
Memory:328 kb
****************************************************************/
const maxm=21; maxn=105;
type etype = record
go,next,w:longint;
end;
var n,m,k,cnt:longint;
head:array[0..maxm] of longint;
flag:array[0..maxn,0..maxm] of boolean;
t:array[0..maxn,0..maxn] of int64;
f:array[0..maxn] of int64;
e:array[0..maxn*8] of etype;
function min(x,y:int64):int64;
begin if x<y then exit(x) else exit(y); end;
procedure insert(u,v,w:longint);
begin inc(cnt); e[cnt].go:=v; e[cnt].w:=w; e[cnt].next:=head[u]; head[u]:=cnt; end;
function spfa(a,b:longint):int64;
var block,used:array[0..maxm] of boolean;
dist:array[0..maxm] of longint;
q:array[0..3*maxn] of longint;
i,j,t,w,now:longint;
begin
fillchar(block,sizeof(block),false);
for i:=1 to m do dist[i]:=maxlongint shr 1;
fillchar(used,sizeof(used),false);
for i:=a to b do
for j:=1 to m do
if flag[i,j] then block[j]:=true;
q[0]:=1; used[1]:=true; dist[1]:=0;
t:=0; w:=1;
while (t<w) do begin
now:=q[t]; inc(t); i:=head[now];
while (i<>0) do begin
if (not block[e[i].go]) and (dist[e[i].go]>dist[now]+e[i].w)
then begin
dist[e[i].go]:=dist[now]+e[i].w;
if (not used[e[i].go]) then begin
q[w]:=e[i].go; used[e[i].go]:=true; inc(w);
end;
end;
i:=e[i].next;
end;
used[now]:=false;
end;
exit(dist[m]);
end;
procedure init();
var q,d,i,j,x,y,z:longint;
begin
readln(n,m,k,q); cnt:=1;
for i:=1 to q do begin
readln(x,y,z); insert(x,y,z); insert(y,x,z);
end;
readln(d);
for i:=1 to d do begin
readln(x,y,z); for j:=y to z do flag[j,x]:=true;
end;
for i:=1 to n do
for j:=1 to n do
t[i,j]:=spfa(i,j);
end;
procedure dp();
var i,j:longint;
begin
for i:=1 to n do begin
f[i]:=t[1,i]*i;
for j:=0 to i-1 do
f[i]:=min(f[i],f[j]+k+t[j+1,i]*(i-j));
end;
end;
begin
init();
dp();
writeln(f[n]);
end.