Exemplo n.º 1
0
        public static void FindMinimalPaths(Dictionary<Node, List<Street>> graph, Node source)
        {
            PriorityQueue<Node> queue = new PriorityQueue<Node>();

            foreach (var node in graph)
            {
                if (source.ID != node.Key.ID)
                {
                    node.Key.DijkstraDistance = ulong.MaxValue;
                }
            }

            source.DijkstraDistance = 0;
            queue.Enqueue(source);

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

                foreach (var neighbour in graph[currentNode])
                {
                    ulong potDistance = currentNode.DijkstraDistance + neighbour.Distance;

                    if (potDistance < neighbour.Node.DijkstraDistance)
                    {
                        neighbour.Node.DijkstraDistance = potDistance;
                        // adds only modified elements in the queue
                        // thus reordering the queue after each iteration is avoided
                        queue.Enqueue(neighbour.Node);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void DijkstraAlgorithm(Node source, Dictionary<Node, List<Connection>> graph)
        {
            PriorityQueue<Node> queue = new PriorityQueue<Node>();

            foreach (var node in graph)
            {
                if (source.PointID != node.Key.PointID)
                {
                    node.Key.DijktraDistance = int.MaxValue;
                    queue.Enqueue(node.Key); 
                }
            }

            source.DijktraDistance = 0;
            queue.Enqueue(source);

            while (queue.Count != 0)
            {
                Node currentNode = queue.Peek();

                if (currentNode.DijktraDistance == int.MaxValue)
                {
                    break;
                }

                foreach (var neighbour in graph[currentNode])
                {
                    int potDistance = currentNode.DijktraDistance + neighbour.Distance;

                    if (potDistance < neighbour.Node.DijktraDistance)
                    {
                        neighbour.Node.DijktraDistance = potDistance;
                        Node next = new Node(neighbour.Node.PointID, potDistance);
                        queue.Enqueue(next);
                    }
                }

                queue.Dequeue();
            }
        }
Exemplo n.º 3
0
        public static void Dijkstra(Node startNode)
        {
            PriorityQueue<Node> queue = new PriorityQueue<Node>();

            foreach (var item in graph)
            {
                if (item.Key.Value != startNode.Value)
                {
                    item.Key.DijkstraDistance = int.MaxValue;
                }
            }

            startNode.DijkstraDistance = 0;

            queue.Enqueue(startNode);

            while (queue.Count > 0)
            {
                var current = queue.Peek();

                if (current.DijkstraDistance == int.MaxValue)
                {
                    break;
                }

                foreach (var neighbour in graph[current])
                {
                    int potentialDistance = current.DijkstraDistance + neighbour.Distance;

                    if (potentialDistance < neighbour.Node.DijkstraDistance)
                    {
                        neighbour.Node.DijkstraDistance = potentialDistance;
                        queue.Enqueue(neighbour.Node);
                    }
                }

                queue.Dequeue();
            }
        }
Exemplo n.º 4
0
        public static void Main()
        {
            graph = new Dictionary<Node, List<Connection>>();
            nodes = new Dictionary<int, Node>();
            hospitals = new List<int>();
            string[] totalsString = Console.ReadLine().Split(' ');
            int connectionsCount = int.Parse(totalsString[1]);

            var hospitalsString = Console.ReadLine().Split(' ');

            foreach (var hospital in hospitalsString)
            {
                int val = int.Parse(hospital);
                Node hospitalNode = new Node(val);
                hospitalNode.IsHospital = true;

                nodes.Add(val, hospitalNode);
                graph.Add(nodes[val], new List<Connection>());
                hospitals.Add(val);
            }

            for (int i = 0; i < connectionsCount; i++)
            {
                string[] input = Console.ReadLine().Split(' ');
                int firstVal = int.Parse(input[0]);
                int secondVal = int.Parse(input[1]);
                int dist = int.Parse(input[2]);


                if (!nodes.ContainsKey(firstVal))
                {
                    nodes.Add(firstVal, new Node(firstVal));
                }

                if (!nodes.ContainsKey(secondVal))
                {
                    nodes.Add(secondVal, new Node(secondVal));
                }

                if (!graph.ContainsKey(nodes[firstVal]))
                {
                    graph.Add(nodes[firstVal], new List<Connection>());
                }

                if (!graph.ContainsKey(nodes[secondVal]))
                {
                    graph.Add(nodes[secondVal], new List<Connection>());
                }

                graph[nodes[firstVal]].Add(new Connection(nodes[secondVal], dist));
                graph[nodes[secondVal]].Add(new Connection(nodes[firstVal], dist));
            }

            int minDistance = int.MaxValue;

            foreach (var hosp in hospitals)
            {
                int currentDist = 0;

                Dijkstra(nodes[hosp]);

                foreach (var item in graph)
                {
                    if (!item.Key.IsHospital)
                    {
                        currentDist += item.Key.DijkstraDistance;
                    }
                }

                minDistance = Math.Min(currentDist, minDistance);
            }

            Console.WriteLine(minDistance);
        }
Exemplo n.º 5
0
 public Connection(Node node, int distance)
 {
     this.Node = node;
     this.Distance = distance;
 }
 public void Enqueue(Node newNode)
 {
     this.elements.Add(newNode);
 }
Exemplo n.º 7
0
        static void Main()
        {
            string line = Console.ReadLine();
            var splittedArgs = line.Split(' ');
            var nodesCount = int.Parse(splittedArgs[0]);
            var connectionCount = int.Parse(splittedArgs[1]);
            var hospitalCount = int.Parse(splittedArgs[2]);

            string secondLine = Console.ReadLine();
            var splitted = secondLine.Split(' ');

            for (var i = 0; i < splitted.Length; i++)
            {
                hospitals.Add(new Node(int.Parse(splitted[i])));
            }

            var graph = new Dictionary<Node,List<Connection>>();

            for (var j = 0; j < connectionCount; j++)
            {
              var input = Console.ReadLine().Split(' ');
              var currentNodeFrom = new Node(int.Parse(input[0]));
              var currentNodeTo = new Node(int.Parse(input[1]));
              var currentWeight = int.Parse(input[2]);

              var currentConnection = new Connection(currentNodeTo,currentWeight);

              if (!graph.ContainsKey(currentNodeFrom))
              {
                  graph[currentNodeFrom] = new List<Connection>();
              }

              graph[currentNodeFrom].Add(currentConnection);

              if (!graph.ContainsKey(currentNodeTo))
              {
                  graph[currentNodeTo] = new List<Connection>();
              }

              graph[currentNodeTo].Add(new Connection(currentNodeFrom, currentWeight));

            }

            var list = new List<double>();
            foreach (var hospital in hospitals)
            {
                DijkstraAlgorithm(graph, hospital);
                double result = 0;

                foreach (var path in graph)
                {
                    if (!hospitals.Contains(path.Key))
                    {
                        result += path.Key.DijkstraDistance;
                    }
                }

                list.Add(result);
            }

            Console.WriteLine(list.Min());
        }
Exemplo n.º 8
0
 public Street(Node node, ulong distance)
 {
     this.Node = node;
     this.Distance = distance;
 }