Esempio n. 1
0
        public static void Main()
        {
            BinaryHeapPriorityQueue<int> heap = new BinaryHeapPriorityQueue<int>(8);

            heap.Enqueue(new Node<int>(5, true));
            heap.Enqueue(new Node<int>(1, true));
            heap.Enqueue(new Node<int>(1, true));
            heap.Enqueue(new Node<int>(1, true));
            heap.DFS();

            heap.DisplayAllValues();
        }
        protected override void OnSetUp() {
            base.OnSetUp();

            _priorityQueue = new BinaryHeapPriorityQueue<int>();

            for(int i = 0; i < ItemCount; i++)
                _priorityQueue.Enqueue(Rnd.Next(ItemCount));
        }
Esempio n. 3
0
        public void MSAGLAstarSSSP(Vertex[] vList, Edge[,] eList, int[] degList, int source, Dictionary <Node, int> nodeId, GeometryGraph _mainGeometryGraph,
                                   int NofNodesBeforeDetour, int n, Tiling g, Tiling g1)
        {
            var q = new BinaryHeapPriorityQueue(n);



            vList[source].Dist = 0;
            q.Enqueue(source, vList[source].Dist);

            for (int i = 0; i < n; i++)
            {
                if (vList[i].Id != source)
                {
                    vList[i].Dist = double.MaxValue;
                    q.Enqueue(i, vList[i].Dist);
                }
                vList[i].Parent  = null;
                vList[i].Visited = false;
            }


            Distance = 0;

            while (q.Count > 0)
            {
                var    deq = q.Dequeue();
                Vertex u   = vList[deq];
                u.Visited = true;
                if (u == null || u.Invalid)
                {
                    return;
                }
                for (int neighb = 0; neighb < degList[u.Id]; neighb++)
                {
                    var neighborId = eList[u.Id, neighb].NodeId;
                    int discourage = 0;

                    if (u.Id < NofNodesBeforeDetour && u.Id != source)
                    {
                        discourage = 1000;
                    }

                    Vertex neighbor = vList[neighborId];
                    double edist    = MsaglUtilities.EucledianDistance(u.XLoc, u.YLoc, neighbor.XLoc, neighbor.YLoc);
                    var    tempDist = u.Dist + edist + discourage;
                    if (tempDist >= neighbor.Dist)
                    {
                        continue;
                    }

                    neighbor.Dist   = tempDist;
                    neighbor.Parent = u;
                    if (neighbor.Visited)
                    {
                        neighbor.Visited = false;
                        q.Enqueue(neighbor.Id, neighbor.Dist);
                    }
                    else
                    {
                        q.DecreasePriority(neighbor.Id, neighbor.Dist);
                    }
                }
            }
            foreach (var node in _mainGeometryGraph.Nodes)
            {
                int target = nodeId[node];
                if (target == source)
                {
                    continue;
                }
                Vertex route     = vList[target];
                int    zoomlevel = Math.Max(vList[source].ZoomLevel, vList[target].ZoomLevel);
                Edgelist.Clear();
                while (route.Parent != null)
                {
                    ShortestPath.Add(route.Id);
                    for (int neighb = 0; neighb < degList[route.Id]; neighb++)
                    {
                        if (eList[route.Id, neighb].NodeId == route.Parent.Id)
                        {
                            SetUsed(vList, eList, degList, route.Id, eList[route.Id, neighb].NodeId, zoomlevel);
                            Edgelist.Add(new VertexNeighbor(route.Id, neighb));
                            break;
                        }
                    }

                    route = route.Parent;
                }
                if (route.Id != source)
                {
                    Debug.WriteLine("path not found");
                }
                foreach (VertexNeighbor vn in Edgelist)
                {
                    g1.AddEdge(vn.A, g.EList[vn.A, vn.Neighbor].NodeId, g.EList[vn.A, vn.Neighbor].Selected, g.EList[vn.A, vn.Neighbor].Used);
                }
            }
            return;
        }
Esempio n. 4
0
        public List <int> MSAGLAstarShortestPath(Vertex[] vList, Edge[,] eList, int[] degList, int source, int target, int n)
        {
            var q = new BinaryHeapPriorityQueue(n);


            vList[source].Dist   = 0;
            vList[source].Weight = vList[source].Dist +
                                   MsaglUtilities.EucledianDistance(
                vList[source].XLoc, vList[source].YLoc,
                vList[target].XLoc, vList[target].YLoc);
            q.Enqueue(source, vList[source].Weight);

            for (int i = 0; i < n; i++)
            {
                if (vList[i].Id != source)
                {
                    vList[i].Dist   = double.MaxValue;
                    vList[i].Weight = double.MaxValue;
                    q.Enqueue(i, vList[i].Weight);
                }
                vList[i].Parent  = null;
                vList[i].Visited = false;
            }

            Edgelist.Clear();
            ShortestPath.Clear();
            Distance = 0;

            while (q.Count > 0)
            {
                var    deq = q.Dequeue();
                Vertex u   = vList[deq];
                u.Visited = true;
                if (u == null || u.Invalid)
                {
                    return(new List <int>());
                }
                for (int neighb = 0; neighb < degList[u.Id]; neighb++)
                {
                    var neighborId = eList[u.Id, neighb].NodeId;
                    int discourage = 0;
                    if (eList[u.Id, neighb].Selected == 1)
                    {
                        discourage = 1000;                                   //continue;
                    }
                    if (eList[u.Id, neighb].NodeId == source || eList[u.Id, neighb].NodeId == target)
                    {
                        discourage = 0;
                    }
                    if (u.Id == source || u.Id == target)
                    {
                        discourage = 0;
                    }

                    Vertex neighbor = vList[neighborId];
                    double edist    = MsaglUtilities.EucledianDistance(u.XLoc, u.YLoc, neighbor.XLoc, neighbor.YLoc);
                    var    tempDist = u.Dist + edist + discourage;
                    if (tempDist >= neighbor.Dist)
                    {
                        continue;
                    }

                    neighbor.Dist = tempDist;
                    var tempWeight = neighbor.Dist + MsaglUtilities.EucledianDistance(vList[target].XLoc, vList[target].YLoc, neighbor.XLoc, neighbor.YLoc);
                    neighbor.Weight = tempWeight;
                    neighbor.Parent = u;
                    if (neighbor.Visited)
                    {
                        neighbor.Visited = false;
                        q.Enqueue(neighbor.Id, neighbor.Weight);
                    }
                    else
                    {
                        q.DecreasePriority(neighbor.Id, neighbor.Weight);
                    }
                }
                if (u.Id == target)
                {
                    break;
                }
            }

            Vertex route     = vList[target];
            int    zoomlevel = Math.Max(vList[source].ZoomLevel, vList[target].ZoomLevel);

            while (route.Parent != null)
            {
                ShortestPath.Add(route.Id);
                for (int neighb = 0; neighb < degList[route.Id]; neighb++)
                {
                    if (eList[route.Id, neighb].NodeId == route.Parent.Id)
                    {
                        SetUsed(vList, eList, degList, route.Id, eList[route.Id, neighb].NodeId, zoomlevel);
                        Edgelist.Add(new VertexNeighbor(route.Id, neighb));

                        Distance += Math.Sqrt((route.XLoc - route.Parent.XLoc) * (route.XLoc - route.Parent.XLoc) + (route.YLoc - route.Parent.YLoc) * (route.YLoc - route.Parent.YLoc));

                        break;
                    }
                }

                route = route.Parent;
            }
            ShortestPath.Add(route.Id);
            if (route.Id != source)
            {
                Debug.WriteLine("path not found");
            }
            return(ShortestPath);
        }
Esempio n. 5
0
        public void Solve()
        {
            var n = sc.Integer();
            var q = sc.Integer();
            var a = new List <long>()
            {
                n
            };

            for (int i = 0; i < q; i++)
            {
                a.Add(sc.Long());
            }
            a.Reverse();
            var A    = new BinaryHeapPriorityQueue();
            var last = -1L;

            foreach (var x in a)
            {
                if (A.Count == 0)
                {
                    A.Enqueue(new KeyValuePair <Number, Number>(x, 1));
                }
                else
                {
                    if (last <= x)
                    {
                        continue;
                    }
                    while (A.Any())
                    {
                        if (A.Peek().Key <= x)
                        {
                            break;
                        }
                        var y = A.Dequeue();
                        var l = y.Key;
                        var v = y.Value;
                        if (A.Any() && A.Peek().Key == l)
                        {
                            var z = A.Dequeue();
                            v += z.Value;
                        }
                        var k = l / x;
                        A.Enqueue(new KeyValuePair <Number, Number>(x, k * v));
                        if (l % x != 0)
                        {
                            A.Enqueue(new KeyValuePair <Number, Number>(l % x, v));
                        }
                    }
                }
                last = x;
            }
            var ans = new long[n + 1];

            while (A.Any())
            {
                var p = A.Dequeue(); ans[p.Key] += p.Value;
            }
            for (int i = n - 1; i >= 0; i--)
            {
                ans[i] += ans[i + 1];
            }

            for (int i = 0; i < n; i++)
            {
                IO.Printer.Out.WriteLine(ans[i + 1]);
            }
        }
Esempio n. 6
0
        public void Solve()
        {
            var pp = new Point(sc.Integer(), sc.Integer());
            var qq = new Point(sc.Integer(), sc.Integer());
            var n  = sc.Integer();
            var P  = new Point[n + 2];

            P[n] = pp; P[n + 1] = qq;
            var R = new int[n + 2];

            for (int i = 0; i < n; i++)
            {
                P[i] = new Point(sc.Integer(), sc.Integer());
                R[i] = sc.Integer();
            }
            var dist = new double[n + 2];

            for (int i = 0; i < n + 2; i++)
            {
                dist[i] = 1e18;
            }
            dist[n] = 0;
            var pq = new BinaryHeapPriorityQueue <KeyValuePair <int, double> >((l, r) => l.Value.CompareTo(r.Value));

            pq.Enqueue(new KeyValuePair <C, double>(n, 0));
            var used = new bool[n + 2];

            foreach (var x in P)
            {
                Debug.WriteLine(x);
            }
            foreach (var x in R)
            {
                Debug.WriteLine(x);
            }
            while (pq.Count > 0)
            {
                var p = pq.Dequeue();
                if (used[p.Key])
                {
                    continue;
                }
                Debug.WriteLine(p);
                used[p.Key] = true;
                for (int i = 0; i < n + 2; i++)
                {
                    if (used[i])
                    {
                        continue;
                    }
                    var cost = get(P[i], P[p.Key], R[i] + R[p.Key]);
                    if (dist[i] > p.Value + cost)
                    {
                        dist[i] = p.Value + cost;
                        pq.Enqueue(new KeyValuePair <C, double>(i, dist[i]));
                    }
                }
            }

            IO.Printer.Out.WriteLine(dist[n + 1]);
        }