ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1811. Dual-SIM Phone

C# решение. Runtime error
Posted by ptb1994 2 Feb 2015 21:57
Вот мой код. показывает Runtime на 4 тесте. не могу разобраться из-за чего.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main()
        {
            bool sol;
            string s = Console.ReadLine();
            string[] sp = s.Split(' ');
            int n = int.Parse(sp[0]);
            int m = int.Parse(sp[1]);
            int q = n + 1;
            int q2 = n + 2;
            int p1, p2, p3;
            int[, ,] mas = new int[q2, q2, q2]; // основной массив для работы.
            for (int i = 0; i < m; ++i)   // заполнение массива с клавиатуры.
            {
                string a = Console.ReadLine();
                string[] b = a.Split(' ');
                int[,] c = new int[1, 3]; // интовый массив вводимой строки

                for (int j = 0; j < 3; ++j)
                {
                    c[0, j] = int.Parse(b[j]);
                    if (j == 2)           // когда массив с заполняется, вынимаем индексы и цены.
                    {
                        p1 = c[0, 0];
                        p2 = c[0, 1];
                        p3 = c[0, 2];
                        mas[p2, p1, 0] = p3; // заполняем 0-ой слой основного массива ценами.
                    }
                }
            }
            for (int i = 1; i <= n; ++i)
            {
                for (int j = 1; j <= n; ++j)
                {
                    sol = true;

                    for (int k = 1; k <= n; ++k)
                    {
                        if (mas[k, i, 0] == 0 & mas[k, j, 0] == 0)
                        {
                            mas[j, i, q] = 0;
                            sol = false;
                        }
                        else
                        {
                            if (mas[k, i, 0] <= mas[k, j, 0] & mas[k, i, 0] != 0)
                            {
                                mas[j, i, k] = mas[k, i, 0];
                            }
                            else
                            {
                                mas[j, i, k] = mas[k, j, 0];
                            }
                            if (mas[j, i, q] < mas[j, i, k])
                            {
                                if (sol) { mas[j, i, q] = mas[j, i, k]; }
                            }
                        }
                    }
                    if (mas[0, 0, 0] == 0)
                    {
                        mas[0, 0, 0] = mas[j, i, q];
                    }
                    else
                    {
                        if (mas[j, i, q] > 0)
                        {
                            if (mas[0, 0, 0] >= mas[j, i, q])
                            {
                                mas[0, 0, 0] = mas[j, i, q];
                            }
                        }
                    }
                }
            }



            if (mas[0, 0, 0] < 1)
            {
                Console.WriteLine("No solution");
            }
            else
            {
                Console.WriteLine(mas[0, 0, 0]);
            }
            Console.ReadKey();
        }
    }
}
Re: C# решение. Runtime error
Posted by _Link 5 Feb 2015 11:48
Я, конечно, не по теме, но зачем ты столько вложенности делаешь? В больших программах это же вообще ад.
Например,
 else
                    {
                        if (mas[j, i, q] > 0)
                        {
                            if (mas[0, 0, 0] >= mas[j, i, q])
                            {
                                mas[0, 0, 0] = mas[j, i, q];
                            }
                        }
                    }
можно заменить на else if((mas[j, i, q] > 0)&&(mas[0, 0, 0] >= mas[j, i, q])) mas[0, 0, 0] = mas[j, i, q];
вообще одна-две строки.