[BZOJ1624][Usaco2008 Open] Clear And Present Danger 寻宝之路

来源:互联网 发布:剑网三毒姐捏脸数据 编辑:程序博客网 时间:2024/05/05 11:09

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1624

题目大意

按照顺序求最短路

题解

SPFA

var p,dist:array[0..100]of longint; w:array[0..100,0..100]of longint; t:array[0..100000]of longint; x:array[0..10000]of longint; i,j,k:longint; n,m:longint; head,tail,v,ans:longint;procedure spfa(a,b:longint);var i:longint;begin fillchar(p,sizeof(p),0); for i:=1 to n do  dist[i]:=1000000000; head:=1; tail:=2; t[1]:=a; dist[a]:=0; while head<tail do  begin   v:=t[head]; inc(head);   for i:=1 to n do    if w[v,i]+dist[v]<dist[i]    then begin     dist[i]:=w[v,i]+dist[v];     if p[i]=0 then begin p[i]:=1; t[tail]:=i; inc(tail); end;    end;   p[v]:=0;  end; inc(ans,dist[b]);end;begin readln(n,m); for i:=1 to m do  readln(x[i]); for i:=1 to n do  for j:=1 to n do   read(w[i,j]); ans:=0; x[0]:=1; for i:=1 to m do  spfa(x[i-1],x[i]); writeln(ans);end.
0 0
原创粉丝点击