Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program hotkeys;
- {$mode objfpc}{$H+}
- uses Sysutils;
- type
- waytoproduce = record
- waytime: Int64;
- usedverticles: string;
- numofverticles: Longint;
- end;
- var
- graph: array of array of Longint; //связи между вершинами
- costs: array of Longint; //стоимости прохода через вершину
- visited: array of boolean; //посещённые в DFS вершины
- i,n,j,k,t: Longint;
- way: waytoproduce; //выходные данные
- function DFS(v: longint; ACost: Int64; ANumOfVert: Longint; AUsedverticles: string): waytoproduce;
- var
- correctpath: boolean;
- begin
- if (not visited[v]) then
- begin
- Result.usedverticles := IntToStr(v+1)+' '+AUsedverticles;
- Result.waytime := ACost + costs[v];
- Result.numofverticles := ANumOfVert + 1;
- visited[v] := true;
- if (length(graph[v])=0) then
- begin
- correctpath:=true;
- for i := low(graph[0]) to high(graph[0]) do
- begin
- if (not visited[graph[0][i]]) then correctpath := false;
- end;
- if (correctpath) then
- begin
- if ((length(way.usedverticles)>0) and (way.waytime > Result.waytime)) or (length(way.usedverticles)=0)then
- begin
- way.usedverticles:=Result.usedverticles;
- way.numofverticles:=Result.numofverticles;
- way.waytime:=Result.waytime;
- exit;
- end;
- end;
- end;
- for i := low(graph[v]) to high(graph[v]) do
- DFS(graph[v][i],Result.waytime,Result.numofverticles,Result.usedverticles);
- visited[v] := false;
- end;
- end;
- begin
- assign(input,'details.in');
- assign(output,'details.out');
- reset(input);
- readln(n);
- setlength(graph,n);
- setlength(costs,n);
- setlength(visited,n);
- for i := 0 to high(costs) do
- read(costs[i]);
- readln;
- for i := 0 to high(graph) do
- begin
- read(k);
- setlength(graph[i],k);
- for j := 0 to k-1 do
- begin
- read(t);
- graph[i][j] := t-1;
- end;
- readln;
- end;
- close(input);
- rewrite(output);
- DFS(0,0,0,'');
- write(way.waytime,' ');
- writeln(way.numofverticles);
- write(way.usedverticles);
- close(output);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement