Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 31.12.2013, 01:28   #1
proef
Форумчанин
 
Регистрация: 08.06.2013
Сообщений: 133
По умолчанию c# метод потенциалов

решается транспортная задача по методу северо - западного угла
как теперь её оптимизировать по методу потенциалов?
не могу придумать как считать потенциалы
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11
{
    class Program
    {
        struct Element {

           public int Delivery{get;set;}
           public int Value   {get; set;}
           public static int  FindMinElement(int a, int b)
            {
                if (a > b) return b;
                if (a == b) { return a; }
                else return a;
            }
           
        }
       
        static void Main(string[] args)
        {
            int i=0;
            int j=0;
            int n;
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("Введите количество A");
           n =  Convert.ToInt32(Console.ReadLine());
            int[] a = new int[n];
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Введите количество B");
            int m = Convert.ToInt32(Console.ReadLine());
            int[] b = new int[m];
            Element[,] C = new Element[n, m];
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("Введите a[i]");
            for (i = 0; i < a.Length; i++) {
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("Введите b[i]");
            for (j = 0; j < b.Length; j++) 
            {
                b[j] = Convert.ToInt32(Console.ReadLine());
            }
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Введите C[i][j]");
            for (i = 0; i < n; i++) { 
            for (j = 0; j < m; j++) 
            {
                Console.Write("a[{0},{1}] = ",i,j);
                Console.ForegroundColor = ConsoleColor.Red;
                C[i, j].Value = Convert.ToInt32(Console.ReadLine());
                Console.ResetColor();
            
            } }
            i = j = 0;
            
            while (i < n && j < m) 
            {
                
                try
                {
                    if (a[i] == 0) { i++; }
                    if (b[j] == 0) { j++; }
                    if (a[i] == 0 && b[j] == 0) { i++; j++; }
                    C[i, j].Delivery = Element.FindMinElement(a[i], b[j]);
                    a[i] -= C[i, j].Delivery;
                    b[j] -= C[i, j].Delivery;
                   }
                catch { }
          }
            for (i = 0; i < n; i++) 
            {
                for (j = 0; j < m; j++) 
                {
                    if (C[i, j].Delivery != 0) { Console.ForegroundColor = ConsoleColor.Blue; 
                        Console.Write("{0}",C[i, j].Value);
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("({0})", C[i, j].Delivery); Console.ResetColor();
                    }
                    else
                    Console.Write("{0}({1})",C[i,j].Value,C[i,j].Delivery);
                }
                Console.WriteLine();
            
            }
            int ResultFunction = 0;

            for (i = 0; i < n; i++) {
                for (j = 0; j < m; j++) 
                {

                    ResultFunction += (C[i, j].Value * C[i, j].Delivery);
                    
                
                }

            
            }
            Console.WriteLine(" Result = {0}", ResultFunction);

            
         
           

            Console.ReadLine();
          

            
            
        }
    }
}
proef вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Транспортная задача.Метод потенциалов Delaforse Общие вопросы Delphi 2 17.05.2013 10:46
Метод потенциалов (Delphi + Excel) salns Помощь студентам 0 22.03.2012 19:03
Метод потенциалов. Delphi. nonsb Помощь студентам 5 05.02.2012 14:43
Метод потенциалов meravingin Помощь студентам 3 06.08.2009 10:53