TYVJ 1520 [树的直径]

求树的最长链,用了SPFA+SLF(表示BFS就可以,作者纯属DT)

 

[pascal 代码]

Const
        maxn=100000;
        maxq=1000000;
VAR
        a,b,e,dis,f:array[1..maxn]of longint;
        q:array[1..maxq]of longint;
        vis:array[1..maxn]of boolean;
        n,m,head,tail,maxdis,maxi,i:longint;
Procedure addgraph(x,y,z:longint);
 begin
        inc(m);
        a[m]:=x;b[m]:=y;e[m]:=z;
 end;
Procedure init;
 var
        i,x,y,z:longint;
 begin
        readln(n);
        for i:=1 to n-1 do
                begin
                        readln(x,y,z);
                        addgraph(x,y,z);
                        addgraph(y,x,z);
                end;
 end;
Procedure qsort(l,r:longint);
 var
        i,j,x,y:longint;
 begin
        i:=l;j:=r;x:=a[(l+r)shr 1];
        Repeat
                while a[i]<x do inc(i);
                while a[j]>x do dec(j);
                if i<=j then
                        begin
                                y:=a[i];a[i]:=a[j];a[j]:=y;
                                y:=b[i];b[i]:=b[j];b[j]:=y;
                                y:=e[i];e[i]:=e[j];e[j]:=y;
                                inc(i);dec(j);
                        end;
        Until i>j;
        if i<r then qsort(i,r);
        if l<j then qsort(l,j);
 end;
Procedure ready;
 var
        i:longint;
 begin
        fillchar(f,sizeof(f),0);
        qsort(1,m);
        for i:=1 to m do if f[a[i]]=0 then f[a[i]]:=i;
        f[n+1]:=m+1;
        for i:=n downto 1 do if f[i]=0 then f[i]:=f[i+1];
 end;
Function outque:longint;
 begin
        inc(head);
        if head>maxq then head:=1;
        exit(q[head]);
 end;
Procedure enque(x:longint); //SLF
 begin
        vis[x]:=true;
        if dis[x]>dis[q[head]] then
                begin
                        inc(tail);
                        if tail>maxq then tail:=1;
                        q[tail]:=x;
                end
        else
                begin
                        dec(head);
                        if head<1 then head:=maxq;
                        q[head]:=x;
                end;
 end;
Procedure spfa(s:longint); //SPFA+SLF
 var
        i,pre:longint;
 begin
        fillchar(vis,sizeof(vis),false);
        fillchar(dis,sizeof(dis),$3f);
        head:=0;tail:=1;q[1]:=s;dis[s]:=0;
        while head<>tail do
                begin
                        pre:=outque;
                        for i:=f[pre] to f[pre+1]-1 do
                                begin
                                        if dis[pre]+e[i]<dis[b[i]] then
                                                begin
                                                        dis[b[i]]:=dis[pre]+e[i];
                                                        if not vis[b[i]] then enque(b[i]);
                                                end;
                                end;
                        vis[pre]:=false;
                end;
 end;
Begin
        init;
        ready;
        spfa(1);
        maxdis:=0;
        for i:=1 to n do if dis[i]>maxdis then
                begin
                        maxdis:=dis[i];
                        maxi:=i;
                end;
        spfa(maxi);
        maxdis:=0;
        for i:=1 to n do if dis[i]>maxdis then maxdis:=dis[i];
        writeln(maxdis);
End.

转载于:https://www.cnblogs.com/FreeDestiny/archive/2011/10/26/2224899.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值