【二进制+SPFA】路由

来源:互联网 发布:c语言表示最大公约数 编辑:程序博客网 时间:2024/06/15 22:42

      

        

================================

=====================

type  node=record         s:longint;         b:array[1..5]of int64;       end;var  n:longint;  a,b:array[1..4]of longint;  map:array[1..90,1..90]of boolean;  map_dis:array[1..90,1..90]of longint;  dis:array[1..90]of longint;  f_bo:array[1..90]of boolean;  road:array[1..90]of node;  dui:array[1..200000]of longint;  g:array[1..90]of longint;procedure init;begin  assign(input,'route.in');  assign(output,'route.out');  reset(input); rewrite(output);end;procedure terminate;begin  close(input); close(output);  halt;end;procedure pre;var  i,j,k,t:longint;  i1,j1:longint;  flag:boolean;  ch:char;begin  readln(n);  fillchar(map,sizeof(map),false);  for i:=1 to n do    begin      readln(k);      road[i].s:=k;      for j:=1 to k do        begin          fillchar(a,sizeof(a),0);          fillchar(b,sizeof(b),0);          flag:=true;          t:=1;          while not eoln do            begin              if flag then                begin                  read(ch);                  if ch=' ' then begin flag:=false; t:=1; continue; end;                  if ch='.' then begin inc(t); continue; end;                  a[t]:=a[t]*10+ord(ch)-ord('0');                end                else                begin                  read(ch);                  if ch='.' then begin inc(t); continue; end;                  b[t]:=b[t]*10+ord(ch)-ord('0');                end;            end;           road[i].b[j]:=(a[1] shl 24+a[2] shl 16 +a[3] shl 8+a[4])and(b[1] shl 24+b[2] shl 16 +b[3] shl 8+b[4]);         readln;        end;    end;  fillchar(map_dis,sizeof(map_dis),$7);  for i:=1 to n do    for j:=1 to n do      if i<>j then        for i1:=1 to road[i].s do          for j1:=1 to road[j].s do            if road[i].b[i1]=road[j].b[j1] then              begin                map[i,j]:=true;                map[j,i]:=true;                map_dis[i,j]:=1;                map_dis[j,i]:=1;              end;end;procedure spfa;var  i:longint;  x:longint;  l,r:longint;  a,b:longint;  procedure print(t:longint);begin  if t<>a then print(g[t]);  write(t,' ');end;begin  readln(a,b);  fillchar(f_bo,sizeof(f_bo),false);  fillchar(dis,sizeof(dis),$7);  f_bo[a]:=true;  dis[a]:=0;  l:=0; r:=1;  dui[1]:=a;  repeat    inc(l);    x:=dui[l];    for i:=1 to n do      if i<>x then      if map[x,i] then        begin          if map_dis[x,i]+dis[x]<dis[i] then            begin              dis[i]:=map_dis[x,i]+dis[x];              g[i]:=x;              if not f_bo[i] then                begin                  f_bo[i]:=true;                  inc(r);                  dui[r]:=i;                end;            end;        end;    f_bo[x]:=false;  until l>=r;  if dis[b]<1000000 then    begin      //writeln()      writeln('Yes');      print(b);    end    else      writeln('No');end;begin  init;  pre;  spfa;  terminate;end.