public static void DijkstraAlgorithm(Dictionary<Node, List<Connection>> graph, Node source)
        {
            var queue = new PriorityQueue<Node>();

            foreach (var node in graph)
            {
                node.Key.DijkstraDistance = double.PositiveInfinity;
            }

            source.DijkstraDistance = 0.0d;
            queue.Enqueue(source);

            while (queue.Count != 0)
            {
                var currentNode = queue.Dequeue();

                if (double.IsPositiveInfinity(currentNode.DijkstraDistance))
                {
                    break;
                }

                foreach (var neighbor in graph[currentNode])
                {
                    var potDistance = currentNode.DijkstraDistance + neighbor.Distance;
                    if (potDistance < neighbor.Node.DijkstraDistance)
                    {
                        neighbor.Node.DijkstraDistance = potDistance;
                        queue.Enqueue(neighbor.Node);
                    }
                }
            }
        }
        public static void Main()
        {
            //Get N, M and H
            string[] input = Console.ReadLine().Split(' ');
            int numberOfNodesN = int.Parse(input[0]);
            int numberOfconnectionsM = int.Parse(input[1]);
            int numberOfHospitalsH = int.Parse(input[2]);

            //Get all hospitals
            string[] hospitals = Console.ReadLine().Split(' ');

            //Populate the graph
            var graph = new Dictionary<Node, List<Connection>>();
            var nodes = new List<Node>();
            for (int i = 1; i <= numberOfNodesN; i++)
            {
                Node node = new Node(i);
                nodes.Add(node);
            }
            for (int i = 0; i < numberOfNodesN; i++)
            {
                graph[nodes[i]] = new List<Connection>();
            }

            //Fill connections
            for (int i = 0; i < numberOfconnectionsM; i++)
            {
                string[] connectionInfo = Console.ReadLine().Split(' ');
                Node firstNode = nodes[int.Parse(connectionInfo[0]) - 1];
                Node secondNode = nodes[int.Parse(connectionInfo[1]) - 1];
                int length = int.Parse(connectionInfo[2]);

                graph[firstNode].Add(new Connection(secondNode, length));

                graph[secondNode].Add(new Connection(firstNode, length));
            }

            double minTotalPath = double.MaxValue;
            double currentPath = 0;
            var houses = graph.Where(node => !hospitals.Contains(node.Key.Id.ToString())).ToList();
            foreach (var hospital in hospitals)
            {
                Node source = nodes[int.Parse(hospital) - 1];

                DijkstraAlgorithm(graph, source);

                currentPath = 0;

                foreach (var house in houses)
                {
                    currentPath += house.Key.DijkstraDistance;
                }

                if (minTotalPath > currentPath)
                {
                    minTotalPath = currentPath;
                }
            }

            Console.WriteLine(minTotalPath);
        }
Example #3
0
        static void DijkstraAlgorithm(Dictionary<Node, List<Connection>> graph, Node source)
        {
            OrderedBag<Node> priorQueue = new OrderedBag<Node>();

            foreach (var node in graph)
            {
                node.Key.DijkstraDistance = long.MaxValue;
            }

            source.DijkstraDistance = 0;
            priorQueue.Add(source);

            while (priorQueue.Count != 0)
            {
                Node currentElement = priorQueue.RemoveFirst();

                if (currentElement.DijkstraDistance == long.MaxValue)
                {
                    break;
                }

                foreach (var connection in graph[currentElement])
                {
                    var posDistance = currentElement.DijkstraDistance + connection.Distance;

                    if (posDistance < connection.ToNode.DijkstraDistance)
                    {
                        connection.ToNode.DijkstraDistance = posDistance;
                        priorQueue.Add(connection.ToNode);
                    }
                }
            }

        }
Example #4
0
 public Connection(Node toNode, int distance)
 {
     this.ToNode = toNode;
     this.Distance = distance;
 }