Listing VIZITA.PAS program trip; const ni='trip.in'; no='trip.out'; nmax=100; huge=2000000000; var f:text; a:array [1..nmax,1..nmax] of Longint; d,s,t,d1,d2:array [1..nmax] of Longint; n,m,i,j,k,h,minim,min,sursa,ns,n1, n2,l1,l2:longint; procedure load; begin Assign(f,ni); Reset(f); Readln(f,n,m); for i:=1 to n do for j:=1 to n do a[i,j]:=huge; for h:=1 to m do begin Readln(f,i,j,k); if k<a[i,j] then begin a[i,j]:=k; a[j,i]:=k end end; Close(f) end; procedure init; begin minim:=huge; end; procedure dijkstra; begin for i:=1 to n do begin d[i]:=a[sursa,i]; if d[i]<>huge then t[i]:=sursa else t[i]:=0; s[i]:=0 end; s[sursa]:=1; for i:=1 to n-1 do begin min:=huge; for j:=1 to n do if s[j]=0 then if d[j]<min then begin min:=d[j]; k:=j end; if min<>huge then begin s[k]:=1; for j:=1 to n do if s[j]=0 then if a[k,j]<>huge then if min+a[k,j]<d[j] then begin d[j]:=min+a[k,j]; t[j]:=k end end end; t[sursa]:=0 end; procedure actua; begin for i:=1 to n-1 do for j:=i+1 to n do if a[i,j]<>huge then if (t[i]<>j)and(t[j]<>i) then if d[i]<>huge then if d[j]<>huge then if a[i,j]+d[i]+d[j] <minim then begin minim:=a[i,j]+ d[i]+d[j]; ns:=sursa; n1:=i; n2:=j end end; procedure work; begin for sursa:=1 to n do begin dijkstra; actua end end; procedure imposibil; begin if minim=huge then begin Assign(f,no);rewrite(f); Writeln(f,'No solution.'); Close(f); Halt end end; procedure reconst; begin sursa:=ns; dijkstra; l1:=0; while n1<>sursa do begin l1:=l1+1; d1[l1]:=n1; n1:=t[n1] end; l1:=l1+1; d1[l1]:=sursa; l2:=0; while n2<>sursa do begin l2:=l2+1; d2[l2]:=n2; n2:=t[n2] end end; procedure scrie; begin Assign(f,no);rewrite(f); for i:=1 to l1 do Write(f,d1[i],' '); for i:=l2 downto 1 do Write(f,d2[i],' '); Writeln(f); Close(f) end; Begin load; init; work; imposibil; reconst; scrie End. [cuprins] |