Depth First search (90 points)
var n,m,k,s,t,i,j,u,v,d,now,res:longint; C,vt,vc:array[0..100]of Longint; A,dist:array[0..100,0..100]of longint;procedure DFS (i:longint); var j:longint;begin if I=t then begin Res:=now ; Exit End For J:=1 to n do if (dist[i,j]<>0) and (a[c[j],c[i]]=0) and (vt[j]=0) and (vc[c[j]]=0) the N Begin Inc (NOW,DIST[I,J]); Vt[j]:=1; Vc[c[j]]:=1; DFS (j); Dec (now,dist[i,j]); vt[j]:=0; vc[c[j]]:=0; End;end;begin assign (input, ' culture.in '); Reset (input); Assign (output, ' culture.out '); Rewrite (output); Read (n,k,m,s,t); For I:=1 to n do read (C[i]); For I:=1-to-K do-j:=1 to-K do read (A[i,j]); For I:=1 to M do begin read (U,V,D); Dist[u,v]:=d; Dist[u,v]:=d; End Res:=maxlongint; Vt[s]:=1; Vc[c[s]]:=1; DFS (S); If Res=maxlongint then Writeln ( -1) Else Writeln (res); Close (input); Close (output); end.
Folyd algorithm
Const Inf=1000000;var n,k,m,s,t,i,j,q,u,v,d:longint; A,DIST:ARRAY[1..110,1..110] of Longint; C:ARRAY[1..110] of Longint;begin assign (input, ' culture.in '); reset (input); Assign (output, ' culture.out '); Rewrite (output); READLN (n,k,m,s,t); For I:=1 to N does for j:=1 to n do dist[i,j]:=inf; For I:=1 to n do read (c[i]); For I:=1-to-K do -j:=1 to -K do read (A[i,j]); For I:=1 to M do begin Readln (u,v,d); If A[c[v],c[u]]=0 then Dist[u,v]:=d; If A[c[u],c[v]]=0 then Dist[v,u]:=d; End; For K:=1 to N does for I:=1 to N does for j:=1 to n does if DIST[I,K]+DIST[K,J]<DIST[I,J] then dist[i,j]: =DIST[I,K]+DIST[K,J]; If Dist[s,t]<inf then Writeln (dist[s,t]) Else Writeln ( -1); Close (input); Close (output); end.
"NOIP2012" second question • Cultural tour