• Travel (Standard IO)


    题意/Description:

           小Q非常喜欢在自己的国家旅行。小Q所在的国家有N座城市,分别编号为1~n,小Q所在的城市编号为1。小Q现在想知道从他所在的城市出发,到其他N-1个城市的最短路程分别是多少?

     

    读入/Input

           第一行两个整数N,M(1<=n<=1000,1<=M<=100000),分别表示小Q所在的国家有N座城市以及城市间有M条单向道路。
          接下来M行,每行三个整数x,y,len(1<=x,y<=n,1<=len<=100000)表示从城市x去到城市y需要走len这么多路程。
           输入可能存在重边

     

    输出/Output

           一共N-1行,每行一个整数,第i个整数表示小Q从城市1到城市(i+1)的最短路程。如果不能到达输出-1

     

    题解/solution

           明显的最短路,SPFA秒掉。注意是单向。

     

    代码/Code

    const
      maxE=100001;
      maxV=400001;
    
    type
      arr=record
        x,y,w,next:int64;
      end;
    
    var
      n,m,nm:longint;
      a:array [0..maxV] of arr;
      ls:array [0..maxE] of longint;
      list,b,d,v:array [0..maxE] of int64;
      ans,max:int64;
    
    procedure spfa;
    var
      i,j,k,h,t:longint;
    begin
      fillchar(d,sizeof(d),63);
      max:=d[1]; h:=0; t:=1;
      v[1]:=1; list[1]:=1; d[1]:=0;
      repeat
        h:=h+1;
        j:=ls[list[h]];
        while j<>0 do
          begin
            with a[j] do
              begin
                if d[x]+w<d[y] then
                  begin
                    d[y]:=d[x]+w;
                    if v[y]=0 then
                      begin
                        t:=t+1;
                        list[t]:=y;
                        v[y]:=1;
                      end;
                  end;
                j:=next;
              end;
          end;
        v[list[h]]:=0;
      until h=t;
    end;
    
    procedure init1;
    begin
      fillchar(ls,sizeof(ls),0);
      fillchar(list,sizeof(list),0);
      fillchar(b,sizeof(b),0);
      fillchar(a,sizeof(a),0);
      fillchar(v,sizeof(v),0);
    end;
    
    procedure init2;
    var
      i,t,k:longint;
    begin
      readln(n,m);
      for i:=1 to m do
        begin
          t:=i*2; k:=t-1;
          with a[k] do
            begin
              readln(x,y,w);
              next:=ls[x];
              ls[x]:=k;
            end;
        end;
      m:=m*2;
    end;
    
    procedure print;
    var
      i:longint;
    begin
      for i:=2 to n do
        writeln(d[i]);
    end;
    
    
    procedure init_main;
    var
      i:longint;
    begin
      init1;
      init2;
      spfa;
      print;
    end;
    
    begin
      init_main;
    end.



  • 相关阅读:
    python读取数据写入excel
    English Study!
    ODOO里视图开发案例---定义一个像tree、form一样的视图
    更改gradle中央仓库,加快访问速度
    hadoop解决集群启动时某个slave的datanode挂掉问题
    ssh免密登录
    大数据集群脚本xcall和xsync
    虚拟机启动后黑屏并无法关闭
    快照与克隆的区别(来自转载)
    VMware12 打不开Centos6.8系统
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319626.html
Copyright © 2020-2023  润新知