首页 > 代码库 > 【ZJOI2017练习&UVA1057】D6T1 Routing(DP,SPFA)

【ZJOI2017练习&UVA1057】D6T1 Routing(DP,SPFA)

题意:给你一个有向图, 并指定起点和终点

问要从起点走向终点, 再从终点走向起点, 最少需要走过多少不同的节点。 

对于 100%的数据, 有 N<=100M<=min(1000,N*N)。 图中可能有重边或者自环

思路:

技术分享

  1 const oo=700000000;
  2 var head1,head2,vet1,vet2,next1,next2:array[1..10000]of longint;
  3     q:array[0..100000]of record
  4                           x,y:longint;
  5                          end;
  6     dis,f:array[1..100,1..100]of longint;
  7     inq:array[1..100,1..100]of boolean;
  8     n,m,i,j,k,cas,tot1,tot2,x,y,z:longint;
  9 
 10 function min(x,y:longint):longint;
 11 begin
 12  if x<y then exit(x);
 13  exit(y);
 14 end;
 15 
 16 procedure spfa;
 17 var i,j,u1,u2,e1,e2,v1,v2,t,w,t1,w1,tmp:longint;
 18 begin
 19  for i:=1 to n do
 20   for j:=1 to n do
 21   begin
 22    inq[i,j]:=false;
 23    dis[i,j]:=oo;
 24   end;
 25  t:=0; w:=1; t1:=0; w1:=1;
 26  q[1].x:=1; q[1].y:=1; inq[1,1]:=true; dis[1,1]:=1;
 27  while t<w do
 28  begin
 29   inc(t); inc(t1);
 30   if t1=(n*n)<<1 then t1:=0;
 31   u1:=q[t1].x; u2:=q[t1].y; inq[u1,u2]:=false;
 32 
 33   e1:=head1[u1];
 34   while e1<>0 do
 35   begin
 36    v1:=vet1[e1];
 37    tmp:=dis[u1,u2];
 38    if v1<>u2 then inc(tmp);
 39    if tmp<dis[v1,u2] then
 40    begin
 41     dis[v1,u2]:=tmp;
 42     if not inq[v1,u2] then
 43     begin
 44      inc(w); inc(w1);
 45      if w1=(n*n)<<1 then w1:=0;
 46      q[w1].x:=v1; q[w1].y:=u2; inq[v1,u2]:=true;
 47     end;
 48    end;
 49    e1:=next1[e1];
 50   end;
 51 
 52   e2:=head2[u2];
 53   while e2<>0 do
 54   begin
 55    v2:=vet2[e2];
 56    tmp:=dis[u1,u2];
 57    if v2<>u1 then inc(tmp);
 58    if tmp<dis[u1,v2] then
 59    begin
 60     dis[u1,v2]:=tmp;
 61     if not inq[u1,v2] then
 62     begin
 63      inc(w); inc(w1);
 64      if w1=(n*n)<<1 then w1:=0;
 65      q[w1].x:=u1; q[w1].y:=v2; inq[u1,v2]:=true;
 66     end;
 67    end;
 68    e2:=next2[e2];
 69   end;
 70 
 71 
 72   if (u1<>u2)and(dis[u1,u2]+f[u1,u2]-1<dis[u2,u1]) then
 73   begin
 74    dis[u2,u1]:=dis[u1,u2]+f[u1,u2]-1;
 75    if not inq[u2,u1] then
 76    begin
 77     inc(w); inc(w1);
 78     if w1=(n*n)<<1 then w1:=0;
 79     q[w1].x:=u2; q[w1].y:=u1; inq[u2,u1]:=true;
 80    end;
 81   end;
 82  end;
 83 end;
 84 
 85 procedure add1(a,b:longint);
 86 begin
 87  inc(tot1);
 88  next1[tot1]:=head1[a];
 89  vet1[tot1]:=b;
 90  head1[a]:=tot1;
 91 end;
 92 
 93 procedure add2(a,b:longint);
 94 begin
 95  inc(tot2);
 96  next2[tot2]:=head2[a];
 97  vet2[tot2]:=b;
 98  head2[a]:=tot2;
 99 end;
100 
101 begin
102  assign(input,uva1057.in); reset(input);
103  assign(output,uva1057.out); rewrite(output);
104  while not eof do
105  begin
106   read(n,m);
107   if n=0 then break;
108   for i:=1 to n do
109   begin
110    head1[i]:=0;
111    head2[i]:=0;
112   end;
113   tot1:=0; tot2:=0;
114   inc(cas);
115   writeln(Network ,cas);
116   for i:=1 to n do
117    for j:=1 to n do
118     if i<>j then f[i,j]:=oo;
119   for i:=1 to m do
120   begin
121    read(x,y);
122    f[x,y]:=1;
123    add1(x,y);
124    add2(y,x);
125   end;
126   for i:=1 to n do
127    for j:=1 to n do
128     for k:=1 to n do f[j,k]:=min(f[j,k],f[j,i]+f[i,k]);
129   if (f[1,2]=oo)or(f[2,1]=oo) then
130   begin
131    writeln(Impossible);
132    writeln;
133    continue;
134   end;
135   spfa;
136   writeln(Minimum number of nodes = ,dis[2,2]);
137   writeln;
138  end;
139  close(input);
140  close(output);
141 end.

 

【ZJOI2017练习&UVA1057】D6T1 Routing(DP,SPFA)