Exemple #1
0
        //???????
        static long Dijkstra(int startIndex)
        {
            Node startNode = nodes[startIndex];
            int  nowIndex  = startIndex;
            var  pQueue    = new PriorityQueueNum();
            var  visiteds  = new bool[nodes.Length];

            for (int i = 0; i < startNode.distances.Length; i++)
            {
                startNode.distances[i] = long.MaxValue;
                visiteds[i]            = false;
            }
            pQueue.Add(0, startIndex);
            startNode.distances[startIndex] = 0;
            long maxDistance = 0;

            while (pQueue.Count > 0)
            {
                KeyValuePair <long, int> pair = pQueue.Dequeue();
                long distance = pair.Key;
                nowIndex = pair.Value;

                if (startNode.distances[nowIndex] < distance)
                {
                    continue;
                }

                Node nowNode = nodes[nowIndex];
                visiteds[nowIndex] = true;
                maxDistance        = Math.Max(maxDistance, distance);

                for (int i = 0; i < nowNode.edges.Count; i++)
                {
                    int nextIndex = nowNode.edges[i].toIndex;
                    if (visiteds[nextIndex])
                    {
                        continue;
                    }

                    long nextDistance = distance + nowNode.edges[i].distance;
                    if (startNode.distances[nextIndex] > nextDistance)
                    {
                        startNode.distances[nextIndex] = nextDistance;
                        pQueue.Add(nextDistance, nextIndex);
                    }
                }
            }
            return(maxDistance);
        }
Exemple #2
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            long[] array    = ReadLongs();
            var    headSums = new long[n + 1];

            headSums[0] = 0;
            var sDictH = new PriorityQueueNum <bool>();

            for (int i = 0; i < n; i++)
            {
                headSums[0] += array[i];
                sDictH.Add(array[i], true);
            }
            for (int i = 1; i <= n; i++)
            {
                headSums[i] = headSums[i - 1] + array[n + i - 1];
                sDictH.Add(array[n + i - 1], true);
                headSums[i] -= sDictH.Dequeue().Key;
            }
            var tailSums = new long[n + 1];

            tailSums[0] = 0;
            var sDictT = new PriorityQueueNum <bool>(true);

            for (int i = 0; i < n; i++)
            {
                tailSums[0] += array[3 * n - i - 1];
                sDictT.Add(array[3 * n - i - 1], true);
            }
            for (int i = 1; i <= n; i++)
            {
                tailSums[i] = tailSums[i - 1] + array[2 * n - i];
                sDictT.Add(array[2 * n - i], true);
                tailSums[i] -= sDictT.Dequeue().Key;
            }

            long res = long.MinValue;

            for (int i = 0; i <= n; i++)
            {
                res = Math.Max(res, headSums[i] - tailSums[n - i]);
            }
            Console.WriteLine(res);
        }
Exemple #3
0
        //???????
        static long Dijkstra(int start)
        {
            Node startNode = nodes[start];
            int  cnt       = 1;
            int  nowIndex  = start;
            var  pQueue    = new PriorityQueueNum();

            for (int i = 0; i < startNode.distances.Length; i++)
            {
                startNode.distances[i] = -1;
            }
            startNode.distances[start] = 0;

            while (cnt < nodes.Length)
            {
                Node nowNode = nodes[nowIndex];
                for (int i = 0; i < nowNode.edges.Count; i++)
                {
                    int destIndex = nowNode.edges[i].toIndex;
                    if (startNode.distances[destIndex] >= 0)
                    {
                        continue;
                    }

                    long distance = startNode.distances[nowIndex]
                                    + nowNode.edges[i].distance;
                    if (pQueue.GetKey(destIndex) > distance)
                    {
                        pQueue.Remove(destIndex);
                        pQueue.Add(distance, destIndex);
                    }
                }

                KeyValuePair <long, int> pair = pQueue.Dequeue();
                nowIndex = pair.Value;
                startNode.distances[nowIndex] = pair.Key;
                cnt++;
            }
            return(startNode.distances[nowIndex]);
        }
Exemple #4
0
        //???????
        static long Dijkstra(int startIndex)
        {
            Node startNode = nodes[startIndex];
            int  cnt       = 1;
            int  nowIndex  = startIndex;
            var  pQueue    = new PriorityQueueNum <int>();
            var  visiteds  = new bool[nodes.Length];

            for (int i = 0; i < startNode.distances.Length; i++)
            {
                startNode.distances[i] = long.MaxValue;
                visiteds[i]            = false;
            }
            //startNode.distances[start] = 0;

            /*
             * while (cnt < nodes.Length)
             * {
             *  Node nowNode = nodes[nowIndex];
             *  for (int i = 0; i < nowNode.edges.Count; i++)
             *  {
             *      int destIndex = nowNode.edges[i].toIndex;
             *      if (startNode.distances[destIndex] >= 0) continue;
             *
             *      long distance = startNode.distances[nowIndex]
             + nowNode.edges[i].distance;
             +      if (pQueue.GetKey(destIndex) > distance)
             +      {
             +          pQueue.Remove(destIndex);
             +          pQueue.Add(distance, destIndex);
             +      }
             +  }
             +
             +  KeyValuePair<long, int> pair=pQueue.Dequeue();
             +  nowIndex = pair.Value;
             +  startNode.distances[nowIndex] = pair.Key;
             +  cnt++;
             + }
             */

            pQueue.Add(0, startIndex);
            startNode.distances[startIndex] = 0;
            while (pQueue.Count > 0)
            {
                KeyValuePair <long, int> pair = pQueue.Dequeue();
                long distance = pair.Key;
                nowIndex = pair.Value;
                if (startNode.distances[nowIndex] < distance)
                {
                    continue;
                }
                Node nowNode = nodes[nowIndex];
                visiteds[nowIndex] = true;
                for (int i = 0; i < nowNode.edges.Count; i++)
                {
                    int nextIndex = nowNode.edges[i].toIndex;
                    if (visiteds[nextIndex])
                    {
                        continue;
                    }

                    long nextDistance = distance + nowNode.edges[i].distance;
                    if (startNode.distances[nextIndex] > nextDistance)
                    {
                        startNode.distances[nextIndex] = nextDistance;
                        pQueue.Add(nextDistance, nextIndex);
                    }
                }
            }

            long maxDistance = 0;

            for (int i = 0; i < nodes.Length; i++)
            {
                maxDistance = Math.Max(maxDistance, startNode.distances[i]);
            }
            return(maxDistance);
        }