【图论】【单源最短路】【SPFA】pascal+邻接表

var list,dis:array[0..10000] of longint;
    next,toit,cost,q:array[0..100000] of longint;
    flag:array[0..100000] of boolean;
    n,m,i,a,b,c,s,e,tot:longint;

procedure add(a,b,c:longint);
begin
    inc(tot);
    toit[tot]:=b; //当前边的出点
    cost[tot]:=c; //边权
    next[tot]:=list[a]; //当前边指向的前一条边
    list[a]:=tot; //当前边的入点a引出的边的标号
end;

procedure SPFA;
var i,head,tail,v,k:longint;
begin
    fillchar(flag,sizeof(flag),true);
    for i:=1 to n do dis[i]:=maxlongint;
    head:=1;
    tail:=1;
    q[1]:=s;
    dis[s]:=0;
    flag[s]:=false;
    repeat
        v:=q[head];
        k:=list[v];
        while k<>0 do begin
            if dis[v]+cost[k]<dis[toit[k]] then begin
                dis[toit[k]]:=dis[v]+cost[k];
                if flag[toit[k]] then begin
                    inc(tail);
                    q[tail]:=toit[k];
                    flag[toit[k]]:=false;
                end;
            end;
            k:=next[k];
        end;
        flag[v]:=true;
        inc(head);
    until head>tail;
end;

begin
    fillchar(list,sizeof(list),0);
    fillchar(next,sizeof(next),0);
    fillchar(toit,sizeof(toit),0);
    fillchar(cost,sizeof(cost),0);
    tot:=0;
    readln(n,m);
    for i:=1 to m do begin
        readln(a,b,c);
        add(a,b,c);
    end;
    readln(s,e);
    SPFA;
    if dis[e]<maxlongint then writeln(dis[e]) else writeln('No Solution');
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值