比赛 20121107 评测结果 AAAAA
题目名称 最难的任务 最终得分 100
用户昵称 张来风飘 运行时间 0.091 s
代码语言 Pascal 内存使用 0.32 MiB
提交时间 2012-11-07 08:33:34
显示代码纯文本
  1. program project1;
  2. var a:array[0..201,0..201] of longint;
  3. d:array[0..201] of longint;
  4. v:array[0..201] of boolean;
  5. t,n,m:longint;
  6. procedure init;
  7. begin
  8. assign(input,'hardest.in');reset(input);
  9. assign(output,'hardest.out');rewrite(output);
  10. read(t);
  11. end;
  12. function dijkstra(s:longint):longint;
  13. var i,u,min,j:longint;
  14. begin
  15. fillchar(v,sizeof(v),0);
  16. v[s]:=true;
  17. for i:=1 to n do d[i]:=a[s,i];
  18. for i:=1 to n-1 do
  19. begin
  20. min:=999999999;u:=-1;
  21. for j:=1 to n do if (not v[j])and(d[j]<min) then
  22. begin
  23. min:=d[j];
  24. u:=j;
  25. end;
  26. if u=-1 then break;
  27. v[u]:=true;
  28. for j:=1 to n do if (not v[j]) and (d[j]>d[u]+a[u,j]) then
  29. d[j]:=d[u]+a[u,j];
  30. end;
  31. if d[n]=999999999 then exit(-1) else exit(d[n]);
  32. end;
  33. procedure main;
  34. var tt,i,j,x,y,w:longint;
  35. begin
  36. for tt:=1 to t do
  37. begin
  38. read(n,m);
  39. for i:=1 to n do
  40. for j:=1 to n do
  41. a[i,j]:=999999999;
  42. for i:=1 to m do
  43. begin
  44. read(x,y,w);
  45. if a[x,y]>w then
  46. a[x,y]:=w;
  47. a[y,x]:=a[x,y];
  48. end;
  49. for i:=1 to n do a[i,i]:=0;
  50. writeln(dijkstra(1));
  51. end;
  52. close(input);
  53. close(output);
  54. end;
  55. begin
  56. init;
  57. main;
  58. end.
  59.