public long Solve(long nodeCount, long[][] edges) { var nodes = new Node[nodeCount]; var nodeConnections = new Dictionary <long, List <(Node, long)> >(); for (int i = 0; i < nodeCount; i++) { nodes[i] = new Node(i + 1, int.MaxValue); nodeConnections.Add(i + 1, new List <(Node, long)>()); } var graph = Q1MinCost.MakeGraph(nodeCount, nodes, edges, nodeConnections); nodes[0].Distance = 0; for (int i = 0; i < nodeCount; i++) { foreach (var node in graph) { foreach (var edge in node.Value) { var source = nodes[node.Key - 1]; var sink = edge.Item1; var weight = edge.Item2; if (sink.Distance > source.Distance + weight) { if (i == nodeCount - 1) { return(1); } sink.Distance = source.Distance + weight; } } } } return(0); }
public string[] Solve(long nodeCount, long[][] edges, long startNode) { var nodes = new Node[nodeCount]; var nodeConnections = new Dictionary <long, List <(Node, long)> >(); for (int i = 0; i < nodeCount; i++) { nodes[i] = new Node(i + 1, int.MaxValue); nodeConnections.Add(i + 1, new List <(Node, long)>()); } nodes[startNode - 1].Distance = 0; var graph = Q1MinCost.MakeGraph(nodeCount, nodes, edges, nodeConnections); var infinityCostNodes = new List <Node>(); var result = new string[nodeCount]; for (int i = 0; i < 2 * nodeCount; i++) { foreach (var node in graph) { foreach (var edge in node.Value) { var source = nodes[node.Key - 1]; var sink = edge.Item1; var weight = edge.Item2; if (sink.Distance > source.Distance + weight) { sink.Distance = source.Distance + weight; sink.Prev = source; if (i >= nodeCount - 1) { result[source.Data - 1] = "-"; result[sink.Data - 1] = "-"; } } } } } var reachableNodes = new List <Node>(); var queue = new Queue <Node>(); queue.Enqueue(nodes[startNode - 1]); var visited = new bool[nodeCount]; while (queue.Count > 0) { var peekNode = queue.Dequeue(); reachableNodes.Add(peekNode); foreach (var node in graph[peekNode.Data]) { if (!visited[node.Item1.Data - 1]) { visited[node.Item1.Data - 1] = true; queue.Enqueue(node.Item1); } } } for (int i = 0; i < nodeCount; i++) { if (!reachableNodes.Contains(nodes[i])) { result[i] = "*"; } else if (result[i] != "-") { result[i] = nodes[i].Distance.ToString(); } } return(result); }