示例#1
0
 public long Solve(long nodeCount, long[][] edges)
 {
     Node[] graph = new Node[nodeCount + 1];
     Q1MinCost.BuildGraph(edges, graph);
     graph[1].Weight = 0;
     return(BellmanFord(graph, edges));
 }
示例#2
0
        public string[] Solve(long nodeCount, long[][] edges, long startNode)
        {
            Node[] graph = new Node[nodeCount + 1];
            Q1MinCost.BuildGraph(edges, graph);
            var        infinitePossible = BellmanFord(graph, edges, startNode);
            List <int> NegativeCycle    = new List <int>();

            while (infinitePossible.Any())
            {
                var temp = infinitePossible.Dequeue();
                TravelBack(graph, temp);
            }
            List <string> result = new List <string>();

            for (int i = 1; i < graph.Length; i++)
            {
                if (graph[i].Weight == Max)
                {
                    result.Add("*");
                }
                else if (graph[i].IsChecked)
                {
                    result.Add("-");
                }
                else
                {
                    result.Add(graph[i].Weight.ToString());
                }
            }

            return(result.ToArray());
        }
        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);
        }