您的位置:首页 > 其它

codevs1021

2015-07-18 12:03 197 查看
题目地址:http://codevs.cn/problem/1021/

分析:

spfa

代码:

const

maxn=1000;

maxe=1000000;

var

d,h,q:array[0..2*maxn]of longint;

v,next,w,pre:array[1..2*maxe]of longint;

f:array[1..maxn]of boolean;

n,m,start,i,k,j,tot,t:longint;

function max(a,b:longint):longint;

begin

if a>b then exit(a) else exit(b);

end;

procedure init;

var x,y,z,i:longint;

begin

readln(n,m);

for i:=1 to m do

begin

readln(x,y,z);

v[i]:=y;

w[i]:=z;

next[i]:=h[x];

h[x]:=i;

v[i+m]:=x;

w[i+m]:=z;

next[i+m]:=h[y];

h[y]:=i+m;

end;

start:=1;

end;

procedure dfs(i:longint);

begin

if i<>1 then dfs(pre[i]);

write(i,' ',w[i],' ');

end;

procedure spfa;

var head,tail,i,p:longint;

begin

fillchar(q,sizeof(q),0);

fillchar(d,sizeof(d),$7f);

head:=0;

tail:=1;

q[1]:=start;

f[start]:=true;

d[start]:=0;

while head<>tail do

begin

head:=(head+1)mod maxn;

i:=q[head];

f[i]:=false;

p:=h[i];

while p>0 do

begin

if d[v[p]]>d[i]+w[p] then

begin

d[v[p]]:=d[i]+w[p];

if not f[v[p]]then

begin

tail:=(tail+1)mod maxn;

q[tail]:=v[p];

f[v[p]]:=true;

end;

pre[v[p]]:=i;

end;

p:=next[p];

end;

end;

end;

procedure work;

var head,tail,p,i:longint;

begin

fillchar(q,sizeof(q),0);

fillchar(d,sizeof(d),$7f);

head:=0;

tail:=1;

q[1]:=start;

f[start]:=true;

d[start]:=0;

while head<>tail do

begin

head:=(head+1)mod maxn;

i:=q[head];

f[i]:=false;

p:=h[i];

while p>0 do

begin

if d[v[p]]>d[i]+w[p] then

begin

d[v[p]]:=d[i]+w[p];

if not f[v[p]]then

begin

tail:=(tail+1)mod maxn;

q[tail]:=v[p];

f[v[p]]:=true;

end;

end;

p:=next[p];

end;

end;

tot:=max(tot,d
);

end;

begin

tot:=0;

init;

spfa;

k:=n;

while k<>1 do

begin

j:=pre[k]; //开pre数组记录所经过的节点

t:=h[j];

while v[t]<>k do t:=next[t];

i:=w[t];

w[t]:=maxlongint div 2;

work;

w[t]:=i;

k:=pre[k];

end;

writeln(tot);

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: