比赛 20121107 评测结果 AAAAA
题目名称 最难的任务 最终得分 100
用户昵称 RoyJames 运行时间 0.085 s
代码语言 Pascal 内存使用 0.43 MiB
提交时间 2012-11-07 11:50:42
显示代码纯文本
  1. program hardest;
  2. const
  3. lim=10000;
  4. var
  5. t,n,m,i,a,b,c,h,cnt,tot:longint;
  6. d,head:array[1..200]of longint;
  7. v:array[1..200]of boolean;
  8. next,e,w:array[1..20000]of longint;
  9. q:array[0..lim]of longint;
  10.  
  11. procedure insert(u,v,len:longint);
  12. begin
  13. inc(tot);
  14. next[tot]:=head[u];
  15. head[u]:=tot;
  16. e[tot]:=v;
  17. w[tot]:=len;
  18. end;
  19.  
  20. begin
  21. assign(input,'hardest.in');
  22. assign(output,'hardest.out');
  23. reset(input);
  24. rewrite(output);
  25. readln(cnt);
  26. while cnt>0 do
  27. begin
  28. readln(n,m);
  29. fillchar(head,sizeof(head),0);
  30. tot:=0;
  31. for i:=1 to m do
  32. begin
  33. readln(a,b,c);
  34. insert(a,b,c);
  35. insert(b,a,c);
  36. end;
  37. for i:=1 to n do d[i]:=maxlongint shr 1;
  38. fillchar(v,sizeof(v),0);
  39. d[1]:=0;
  40. h:=0;
  41. t:=1;
  42. q[1]:=1;
  43. v[1]:=true;
  44. while h<t do
  45. begin
  46. h:=(h+1)mod lim;
  47. i:=head[q[h]];
  48. while i<>0 do
  49. begin
  50. if d[e[i]]>d[q[h]]+w[i] then
  51. begin
  52. d[e[i]]:=d[q[h]]+w[i];
  53. if not v[e[i]]then
  54. begin
  55. t:=(t+1)mod lim;
  56. q[t]:=e[i];
  57. v[e[i]]:=true;
  58. end;
  59. end;
  60. i:=next[i];
  61. end;
  62. v[q[h]]:=false;
  63. end;
  64. if d[n]<maxlongint shr 1 then writeln(d[n])else writeln(-1);
  65. dec(cnt);
  66. end;
  67. close(input);
  68. close(output);
  69. end.
  70.