Esempio n. 1
0
        static void DatePreparation()
        {
            m = int.Parse(reader.ReadLine()); // кількість рядків, що відповідають виробникам    a
            n = int.Parse(reader.ReadLine()); // кількість стовпців, що відповідають споживачам  b
            a = new List <int>();
            b = new List <int>();

            int           sum1 = 0, sum2 = 0;
            List <string> CostList = new List <string>();

            // обробка масиву а
            string str = reader.ReadLine();

            foreach (string s in str.Split(' '))
            {
                a.Add(int.Parse(s));
            }
            foreach (int i in a)
            {
                sum1 += i;
            }

            // обробка масиву b
            str = reader.ReadLine();
            foreach (string s in str.Split(' '))
            {
                b.Add(int.Parse(s));
            }
            foreach (int j in b)
            {
                sum2 += j;
            }

            for (int i = 0; i < m; ++i)
            {
                CostList.Add(reader.ReadLine());
            }
            reader.Close();

            if (sum1 != sum2)
            {
                if (sum1 < sum2)
                {
                    a.Add(sum2 - sum1);
                }
                else
                {
                    b.Add(sum1 - sum2);
                }
            }

            cost = new int[a.Count, b.Count];
            string[] tmp = new string[n];

            for (int i = 0; i < m; ++i)
            {
                tmp = CostList[i].Split(' ');
                for (int j = 0; j < n; ++j)
                {
                    cost[i, j] = int.Parse(tmp[j]);
                }
            }
            m = a.Count;
            n = b.Count;

            table = new cell[m, n];

            for (int i = 0; i < m; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    table[i, j] = new cell(i, j, cost[i, j], M, M, M, M);
                }
            }
        }
Esempio n. 2
0
        static void BuildCycle(int i0, int j0)
        {
            List <int> arr_v = new List <int>();
            List <int> arr_u = new List <int>();

            for (int k = 0; k < m; ++k)
            {
                arr_u.Add(0);
            }
            for (int k = 0; k < n; ++k)
            {
                arr_v.Add(0);
            }

            List <cell> before_cycle = new List <cell>();
            List <cell> cycle        = new List <cell>();
            int         min_value    = M; // те зачення, яке відніматимемо в циклі

            begin_cycle = new cell();

            for (int i = 0; i < m; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (table[i, j].x != M)
                    {
                        arr_u[i] += 1;
                        arr_v[j] += 1;
                    }
                }
            }
            int counter = 0;

            while (counter < 3)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (arr_v[j] == 1)
                    {
                        for (int i = 0; i < m; ++i)
                        {
                            table[i, j].isCross = true;
                            if (table[i, j].x != M)
                            {
                                arr_u[i] -= 1;
                            }
                        }
                        arr_v[j] = 0;
                    }
                }
                for (int i = 0; i < m; ++i)
                {
                    if (arr_u[i] == 1)
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            table[i, j].isCross = true;
                            if (table[i, j].x != M)
                            {
                                arr_v[j] -= 1;
                            }
                        }
                        arr_u[i] = 0;
                    }
                }
                counter++;
            }
            writer.WriteLine("Маска базисного розв'язку");
            for (int i = 0; i < m; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (table[i, j].isCross == true)
                    {
                        writer.Write(" - ");
                    }
                    else if (table[i, j].x != M && table[i, j].isCross == false)
                    {
                        writer.Write(" * ");
                        before_cycle.Add(table[i, j]);
                    }
                    else
                    {
                        writer.Write(" M ");
                    }
                }
                writer.WriteLine();
            }


            begin_cycle = table[i0, j0];
            while (before_cycle.Count != 0)
            {
                for (int k = 0; k < before_cycle.Count; ++k)
                {
                    if (begin_cycle == before_cycle[k])
                    {
                        cycle.Add(begin_cycle);
                        before_cycle.RemoveAt(k);
                        for (int t = 0; t < before_cycle.Count; ++t)
                        {
                            if (before_cycle[t].i == begin_cycle.i || before_cycle[t].j == begin_cycle.j)
                            {
                                begin_cycle = before_cycle[t];
                                break;
                            }
                        }
                    }
                }
            }

            writer.WriteLine("Цикл: ");
            for (int k = 0; k < cycle.Count; ++k)
            {
                if (k % 2 == 0)
                {
                    cycle[k].sign = '+';
                }
                else
                {
                    if (min_value > cycle[k].x)
                    {
                        min_value = cycle[k].x;
                    }
                }
            }
            for (int k = 0; k < cycle.Count - 1; ++k)
            {
                if (k % 2 == 0)
                {
                    writer.Write("'+'{0} -> ", cycle[k].x);
                }
                else
                {
                    writer.Write("'-'{0} -> ", cycle[k].x);
                }
            }
            writer.WriteLine("'-'{0}", cycle[cycle.Count - 1].x);
            int count_unbased = 0;

            for (int k = 0; k < cycle.Count; ++k)
            {
                if (k % 2 == 0)
                {
                    cycle[k].x += min_value;
                }
                else
                {
                    cycle[k].x -= min_value;
                    if (cycle[k].x == 0 && count_unbased == 0)
                    {
                        cycle[k].x = M;
                        table[cycle[k].i, cycle[k].j].x       = M;
                        table[cycle[k].i, cycle[k].j].isBased = false;
                        count_unbased++;
                    }
                }
            }
        }