Beispiel #1
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(h, w) = inputStream.ReadValue <int, int>();
            var panels = InitializeMap(inputStream, h, w);
            var start  = panels[0][0];
            var goal   = panels[10][0];

            const int Inf    = 1 << 28;
            var       counts = new int[h, w, 11].SetAll((i, j, k) => Inf);

            counts[start.row, start.column, 0] = 0;

            if (panels.Any(l => l.Count == 0))
            {
                yield return(-1);

                yield break;
            }

            for (int current = 0; current < 10; current++)
            {
                foreach (var begin in panels[current])
                {
                    foreach (var end in panels[current + 1])
                    {
                        var distance = Math.Abs(begin.row - end.row) + Math.Abs(begin.column - end.column);
                        AlgorithmHelpers.UpdateWhenSmall(ref counts[end.row, end.column, current + 1], counts[begin.row, begin.column, current] + distance);
                    }
                }
            }

            yield return(counts[goal.row, goal.column, 10]);
        }
Beispiel #2
0
 void Update(int[][] distances, int from, int to, int d)
 {
     for (int i = 0; i < distances.Length; i++)
     {
         for (int j = 0; j < distances[i].Length; j++)
         {
             AlgorithmHelpers.UpdateWhenSmall(ref distances[i][j], distances[i][from] + d + distances[to][j]);
             AlgorithmHelpers.UpdateWhenSmall(ref distances[i][j], distances[i][to] + d + distances[from][j]);
         }
     }
 }
Beispiel #3
0
 void WarshallFloyd(int[][] distances)
 {
     for (int k = 0; k < distances.Length; k++)
     {
         for (int i = 0; i < distances.Length; i++)
         {
             for (int j = 0; j < distances[i].Length; j++)
             {
                 AlgorithmHelpers.UpdateWhenSmall(ref distances[i][j], distances[i][k] + distances[k][j]);
             }
         }
     }
 }
Beispiel #4
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(hurdlesCount, length) = inputStream.ReadValue <int, int>();
            var hardles = inputStream.ReadIntArray();

            var(t1, t2, t3) = inputStream.ReadValue <int, int, int>();

            var fastestTime = Enumerable.Repeat(1L << 50, length + 1).ToArray();

            fastestTime[0] = 0;

            for (int i = 0; i < length; i++)
            {
                var loss = Array.BinarySearch(hardles, i) >= 0 ? t3 : 0;

                // 走る
                AlgorithmHelpers.UpdateWhenSmall(ref fastestTime[i + 1], fastestTime[i] + t1 + loss);

                // 1飛ぶ
                if (i + 2 <= length)
                {
                    AlgorithmHelpers.UpdateWhenSmall(ref fastestTime[i + 2], fastestTime[i] + t1 + t2 + loss);
                }
                else
                {
                    AlgorithmHelpers.UpdateWhenSmall(ref fastestTime[i + 1], fastestTime[i] + (t1 + t2) / 2 + loss);
                }

                // 3飛ぶ
                if (i + 4 <= length)
                {
                    AlgorithmHelpers.UpdateWhenSmall(ref fastestTime[i + 4], fastestTime[i] + t1 + t2 * 3 + loss);
                }
                else
                {
                    var toJumpDoubled = length * 2 - i * 2 - 1;
                    AlgorithmHelpers.UpdateWhenSmall(ref fastestTime[length], fastestTime[i] + (t1 + toJumpDoubled * t2) / 2 + loss);
                }
            }

            yield return(fastestTime[length]);
        }
Beispiel #5
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            _ = inputStream.ReadInt();
            var        s            = inputStream.ReadLine();
            var        reverseCosts = inputStream.ReadLongArray();
            var        eraseCosts   = inputStream.ReadLongArray();
            const long Inf          = 1L << 60;

            var costs = new long[s.Length + 1, s.Length + 1].SetAll((i, j) => Inf);

            costs[0, 0] = 0;

            for (int cursor = 0; cursor < s.Length; cursor++)
            {
                for (int height = 0; height < s.Length; height++)
                {
                    var bracket = s[cursor] == '(' ? 1 : -1;

                    // do nothing
                    if (height + bracket >= 0)
                    {
                        AlgorithmHelpers.UpdateWhenSmall(ref costs[cursor + 1, height + bracket], costs[cursor, height]);
                    }

                    // reverse
                    if (height - bracket >= 0)
                    {
                        AlgorithmHelpers.UpdateWhenSmall(ref costs[cursor + 1, height - bracket], costs[cursor, height] + reverseCosts[cursor]);
                    }

                    // erase
                    AlgorithmHelpers.UpdateWhenSmall(ref costs[cursor + 1, height], costs[cursor, height] + eraseCosts[cursor]);
                }
            }

            yield return(costs[s.Length, 0]);
        }
Beispiel #6
0
        long GetMinCost(long n)
        {
            if (memo.ContainsKey(n))
            {
                return(memo[n]);
            }
            else if (n == 0)
            {
                return(0);
            }
            else if (n == 1)
            {
                return(d);
            }
            else
            {
                var min = long.MaxValue;
                if (new BigInteger(n) * d < long.MaxValue)
                {
                    min = n * d;
                }

                var mod2 = n % 2;
                var mod3 = n % 3;
                var mod5 = n % 5;

                AlgorithmHelpers.UpdateWhenSmall(ref min, GetMinCost((n + 1) / 2) + a + (2 - mod2) * d);
                AlgorithmHelpers.UpdateWhenSmall(ref min, GetMinCost(n / 2) + a + mod2 * d);
                AlgorithmHelpers.UpdateWhenSmall(ref min, GetMinCost((n + 2) / 3) + b + (3 - mod3) * d);
                AlgorithmHelpers.UpdateWhenSmall(ref min, GetMinCost(n / 3) + b + mod3 * d);
                AlgorithmHelpers.UpdateWhenSmall(ref min, GetMinCost((n + 4) / 5) + c + (5 - mod5) * d);
                AlgorithmHelpers.UpdateWhenSmall(ref min, GetMinCost(n / 5) + c + mod5 * d);

                return(memo[n] = min);
            }
        }
 public void UpdateWhenSmallTest(int first, int second, int expected)
 {
     AlgorithmHelpers.UpdateWhenSmall(ref first, second);
     Assert.Equal(expected, first);
 }