private static void FindTimeRecursive(Node task, HashSet<Connection> used) { foreach (var connection in task.Connections) { if (used.Contains(connection)) { Console.WriteLine(-1); Environment.Exit(0); } if (connection.TargetNode.TimeToWait < task.Duration + task.TimeToWait) { connection.TargetNode.TimeToWait = task.Duration + task.TimeToWait; var copyOfUsed = new HashSet<Connection>(used); copyOfUsed.Add(connection); FindTimeRecursive(connection.TargetNode, copyOfUsed); } } var childFinishTime = task.TimeToWait + task.Duration; if (maxTime < childFinishTime) { maxTime = childFinishTime; } }
static void Main(string[] args) { var graph1 = new Graph(); var graph = new Graph(); var n = int.Parse(Console.ReadLine()); var nodeValues = Console.ReadLine().Split(' ').Select(int.Parse); foreach (var value in nodeValues) { var node = new Node(value); graph.Nodes.Add(node); var node1 = new Node(value); graph1.Nodes.Add(node1); } for (int i = 0; i < n; i++) { var parents = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); if (parents[0] == 0) { continue; } else { foreach (var parent in parents) { graph.Nodes[i].Parents.Add(graph.Nodes[parent - 1]); graph1.Nodes[i].Parents.Add(graph1.Nodes[parent - 1]); } } } var topologicalOrder = TopologicalSort(graph1); if (topologicalOrder.Count == 0) { Console.WriteLine(-1); return; } Console.WriteLine(MinTime(graph, topologicalOrder)); }
public Connection(Node target) { this.TargetNode = target; }