比赛 20120703 评测结果 AWWWWWAWWW
题目名称 旅行 最终得分 20
用户昵称 fuhao 运行时间 0.012 s
代码语言 Pascal 内存使用 0.22 MiB
提交时间 2012-07-03 09:33:49
显示代码纯文本
const maxn=101; maxk=11;
var
 n,m,k,i,x,y,z,ans,j,kk:longint;
 w:array[0..maxn,0..maxn] of longint;
 u:array[0..maxn] of boolean;
 can:array[0..maxn,0..maxn] of boolean;
 dis:array[0..maxk,-1..maxn] of longint;
 procedure dijkstra;
 var i,j,t,kk:longint;
 begin
  fillchar(dis,sizeof(dis),$7f div 2);
  dis[0,0]:=0;
  for t:=0 to k do
  begin
  fillchar(u,sizeof(u),#0);
  for i:=1 to n do
   begin
    kk:=-1;
    for j:=0 to n-1 do
     if (dis[t,j]<dis[t,kk]) and (not u[j]) then kk:=j;
    if kk=-1 then break;
    u[kk]:=true;
    for j:=0 to n-1 do
     if can[kk,j] and can[j,kk] then
      if dis[t,j]>dis[t,kk]+w[kk,j] then
       dis[t,j]:=dis[t,kk]+w[kk,j] else
     else
      if dis[t+1,j]>dis[t,kk]+2*w[kk,j] then
       dis[t+1,j]:=dis[t,kk]+2*w[kk,j];
   end;
  end;
 end;
begin
 assign(input,'travela.in'); reset(input);
 assign(output,'travela.out'); rewrite(output);
 read(n,m,k);
 fillchar(w,sizeof(w),$7f div 2);
 for i:=1 to m do
  begin
   read(x,y,z);
   if w[x,y]>z then w[x,y]:=z;
   can[x,y]:=true;
  end;
 for i:=0 to n-1 do can[i,i]:=true;
 for kk:=0 to n-1 do
  for i:=0 to n-1 do
   for j:=0 to n-1 do
    can[i,j]:=can[i,j] or (can[i,kk] and can[kk,j]);
 dijkstra;
 ans:=dis[0,-1];
 for i:=0 to k do
  if dis[i,n-1]<ans then ans:=dis[i,n-1];
 if ans=dis[0,-1] then writeln(-1) else writeln(ans);
 close(input); close(output);
end.