Advertisement
boriswinner

Untitled

Jan 11th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.18 KB | None | 0 0
  1. program hotkeys;
  2. {$mode objfpc}{$H+}
  3. uses Sysutils;
  4. type
  5.   waytoproduce = record
  6.     waytime: Int64;
  7.     usedverticles: string;
  8.     numofverticles: Longint;
  9.   end;
  10.  
  11. var
  12.   graph: array of array of Longint; //связи между вершинами
  13.   costs: array of Longint; //стоимости прохода через вершину
  14.   visited: array of boolean; //посещённые в DFS вершины
  15.   i,n,j,k,t: Longint;
  16.   way: waytoproduce; //выходные данные
  17. function DFS(v: longint; ACost: Int64; ANumOfVert: Longint; AUsedverticles: string): waytoproduce;
  18. var
  19.   correctpath: boolean;
  20. begin
  21.   if (not visited[v]) then
  22.   begin
  23.     Result.usedverticles :=  IntToStr(v+1)+' '+AUsedverticles;
  24.     Result.waytime := ACost + costs[v];
  25.     Result.numofverticles := ANumOfVert + 1;
  26.     visited[v] := true;
  27.     if (length(graph[v])=0) then
  28.     begin
  29.       correctpath:=true;
  30.       for i := low(graph[0]) to high(graph[0]) do
  31.       begin
  32.         if (not visited[graph[0][i]]) then correctpath := false;
  33.       end;
  34.       if (correctpath) then
  35.       begin
  36.         if ((length(way.usedverticles)>0) and (way.waytime > Result.waytime)) or (length(way.usedverticles)=0)then
  37.         begin
  38.           way.usedverticles:=Result.usedverticles;
  39.           way.numofverticles:=Result.numofverticles;
  40.           way.waytime:=Result.waytime;
  41.           exit;
  42.         end;
  43.       end;
  44.     end;
  45.     for i := low(graph[v]) to high(graph[v]) do
  46.       DFS(graph[v][i],Result.waytime,Result.numofverticles,Result.usedverticles);
  47.     visited[v] := false;
  48.   end;
  49. end;
  50. begin
  51.   assign(input,'details.in');
  52.   assign(output,'details.out');
  53.   reset(input);
  54.   readln(n);
  55.   setlength(graph,n);
  56.   setlength(costs,n);
  57.   setlength(visited,n);
  58.   for i := 0 to high(costs) do
  59.     read(costs[i]);
  60.   readln;
  61.   for i := 0 to high(graph) do
  62.     begin
  63.       read(k);
  64.       setlength(graph[i],k);
  65.       for j := 0 to k-1 do
  66.       begin
  67.         read(t);
  68.         graph[i][j] := t-1;
  69.       end;
  70.       readln;
  71.     end;
  72.   close(input);
  73.  
  74.   rewrite(output);
  75.   DFS(0,0,0,'');
  76.   write(way.waytime,' ');
  77.   writeln(way.numofverticles);
  78.   write(way.usedverticles);
  79.   close(output);
  80. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement