Transportation problem

From Rosetta Code
Revision as of 12:09, 2 December 2020 by rosettacode>Dylanomics (Undo revision 317425 by Dylanomics (talk))
Transportation problem is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The transportation problem in linear programming is to find the optimal transportation plan for certain volumes of resources from suppliers to consumers, taking into account the cost of transportation. The plan is a table (matrix), whose rows and columns correspond to the suppliers and consumers, the cells are placed in cargo volume.

Example of the transportation problem:

Consumer 1,
need 20 kg
Consumer 2,
need 30 kg
Consumer 3,
need 10 kg
Supplier 1,
supply 25 kg
$3 per kg $5 per kg $7 per kg
Supplier 2,
supply 35 kg
$3 per kg $2 per kg $5 per kg



The object is to solve the classical transport problem using the method of potentials (with redistributive cycle) with the preparation of the initial transportation plan by the north-west corner of the features to be implemented in this task. The input is the number of suppliers and customers, inventory levels, needs and cost matrix transport cargo. The output of the program is the optimal plan. If necessary, enter a fictitious vendor or customer.

The solution for the above example would be the plan:

Consumer 1 Consumer 2 Consumer 3
Supplier 1 20 kg - 5 kg
Supplier 2 - 30 kg 5 kg



See also



Related tasks



1C

<lang>// based on the program of <romix>

перем m,n; // Table size перем u,v; перем БазисныеЯчейки; перем iЦикл, jЦикл; перем Цены, Спрос, Предложение, Отгрузки; // Arrays of the transportation problem перем i1, j1; перем СпросОстаток, ПредложениеОстаток; перем гл_сч; перем гсч;

Функция РаспределениеМетодомСевероЗападногоУгла()

   Для j=1 по n Цикл
       СпросОстаток[j]=Спрос[j];
   КонецЦикла;
   Для i=1 по m Цикл
       ПредложениеОстаток[i]=Предложение[i];
   КонецЦикла;
   Для i=1 по m Цикл
       Для j=1 по n Цикл
           БазисныеЯчейки[i][j]=0;
           Отгрузки[i][j]=0;
       КонецЦикла;    
   КонецЦикла;
   Для i=1 по m Цикл
       Для j=1 по n Цикл
           Если ПредложениеОстаток[i]=0 Тогда
               Прервать;
           ИначеЕсли ПредложениеОстаток[i]<0 Тогда
               ВызватьИсключение("Error: balance of the offer less than 0");
           КонецЕсли;
           чОбъем=СпросОстаток[j];
           Если чОбъем=0 Тогда
               Продолжить;
           ИначеЕсли чОбъем<0 Тогда    
               ВызватьИсключение("Error: balance of the demand less than 0");
           КонецЕсли;
           Если ПредложениеОстаток[i]<чОбъем Тогда
               чОбъем=ПредложениеОстаток[i];
           КонецЕсли; 
           СпросОстаток[j]=СпросОстаток[j]-чОбъем;
           ПредложениеОстаток[i]=ПредложениеОстаток[i]-чОбъем;
           БазисныеЯчейки[i][j]=1;
           Отгрузки[i][j]=чОбъем;
       КонецЦикла;    
   КонецЦикла;    

КонецФункции

Функция ПроверкаПравильностиОтгрузок()

   Для i=1 по m Цикл
       стр="Отгрузки: ";
       Для j=1 по n Цикл
           стр=стр+Отгрузки[i][j]+" ";
       КонецЦикла;    
       Сообщить(стр);
   КонецЦикла;        
   Для i=1 по m Цикл
       чОбъем=0;
       Для j=1 по n Цикл
           чОбъем=чОбъем+Отгрузки[i][j];
       КонецЦикла;    
       Если чОбъем<>Предложение[i] Тогда
           ВызватьИсключение("Error: shipment on the line does not equal the proposal in the row "+i);
       КонецЕсли;
   КонецЦикла;    
   Для j=1 по n Цикл
       чОбъем=0;
       Для i=1 по m Цикл
           чОбъем=чОбъем+Отгрузки[i][j];
       КонецЦикла;    
       Если чОбъем<>Спрос[j] Тогда
           ВызватьИсключение("Error: shipment by the column does not equal to the demand in the column "+j);
       КонецЕсли;
   КонецЦикла;    
   Возврат Истина;

КонецФункции

Функция ВычислениеПотенциалов()

   перем i, j;
   Для i=1 по m Цикл
       u[i]=НеОпределено;
   КонецЦикла;
   Для j=1 по n Цикл
       v[j]=НеОпределено;
   КонецЦикла;
   u[1]=0;
   гл_сч=m*n;
   ВычислениеПотенциаловПоГоризонтали(1);
   Для i=1 по m Цикл
       Если u[i]=НеОпределено Тогда
           Сообщить("Failed to evaluate the potential u["+i+"]");
           Возврат Ложь;
       КонецЕсли;    
   КонецЦикла;
   Для j=1 по n Цикл
       Если v[j]=НеОпределено Тогда
           Сообщить("Failed to evaluate the potential v["+j+"]");
           Возврат Ложь;
       КонецЕсли;    
   КонецЦикла;
   Возврат Истина;

КонецФункции

Функция ВычислениеПотенциаловПоВертикали(j)

   Если v[j]=НеОпределено Тогда 
       ВызватьИсключение("Failed to get the potential v["+j+"]");
   КонецЕсли;    
   Для i=1 по m Цикл
       Если БазисныеЯчейки[i][j]=0 Тогда
           Продолжить;
       КонецЕсли;
       Если u[i]<>НеОпределено Тогда
           Продолжить;
       Иначе
           u[i]=Цены[i][j]-v[j];
           ВычислениеПотенциаловПоГоризонтали(i);
       КонецЕсли;    
   КонецЦикла;    

КонецФункции

Функция ВычислениеПотенциаловПоГоризонтали(i)

   гл_сч=гл_сч-1;
   Если гл_сч=0 Тогда
       ВызватьИсключение("Looping in the calculation of potential");
   КонецЕсли;    
   Если u[i]=НеОпределено Тогда 
       ВызватьИсключение("Failed to get potential u["+i+"]");
   КонецЕсли;    
   Для j=1 по n Цикл
       Если БазисныеЯчейки[i][j]=0 Тогда
           Продолжить;
       КонецЕсли;
       Если v[j]<>НеОпределено Тогда
           Продолжить;
       Иначе
           v[j]=Цены[i][j]-u[i];
           ВычислениеПотенциаловПоВертикали(j);
       КонецЕсли;    
   КонецЦикла;    

КонецФункции

Функция ПроверкаОптимальности()

   перем чРешениеОптимально, чМинимальнаяДельта, i, j, Дельта;
   чРешениеОптимально=Истина;
   чМинимальнаяДельта=НеОпределено;
   Для i=1 по m Цикл
       стр="Дельта=";
       Для j=1 по n Цикл
           Если БазисныеЯчейки[i][j]=1 Тогда
               Дельта=0;
           Иначе
               Дельта = Цены[i][j]-u[i]-v[j];
           КонецЕсли;
           стр=стр+Дельта+" ";
           Если Дельта<0 Тогда
               чРешениеОптимально=Ложь;
           КонецЕсли;
           Если чМинимальнаяДельта=НеОпределено Тогда
               чМинимальнаяДельта=Дельта;
               i1=i;
               j1=j;
           Иначе
               Если Дельта<чМинимальнаяДельта Тогда
                   чМинимальнаяДельта=Дельта;
                   i1=i;
                   j1=j;
               КонецЕсли;        
           КонецЕсли;    
       КонецЦикла;    
   КонецЦикла;
   Возврат чРешениеОптимально;

КонецФункции

Функция СтоимостьПеревозки()

   чСумма=0;
   Для i=1 по m Цикл
       Для j=1 по n Цикл
           чСумма=чСумма+(Отгрузки[i][j]*Цены[i][j]);
       КонецЦикла;    
   КонецЦикла;
   Возврат чСумма;

КонецФункции

Функция ПоискНулевойЯчейкиДляВводаВБазис()

   ок=0;
   Для i=1 по m Цикл
       Для j=1 по n Цикл
           Если БазисныеЯчейки[i][j]=0 Тогда
               ок=1;
               Прервать;
           КонецЕсли;
       КонецЦикла;
       Если ок=1 Тогда
           Прервать;
       КонецЕсли;
   КонецЦикла;    
   Если ок=0 Тогда
       ВызватьИсключение("There is no nonbasic (zero) cell entry into the basis");
   КонецЕсли;
   Пока 1=1 Цикл
       i=ГСЧ.СлучайноеЧисло(1, m);
       j=ГСЧ.СлучайноеЧисло(1, n);
       Если БазисныеЯчейки[i][j]=1 Тогда
           Продолжить;
       КонецЕсли;
       Если Отгрузки[i][j]<>0 Тогда
           ВызватьИсключение("Nonzero shipment for nonbasic cell");
       КонецЕсли;
       БазисныеЯчейки[i][j]=1;
       Сообщить("В базис введена ячейка "+i+" "+j);
       Возврат Истина;
   КонецЦикла;

КонецФункции

Функция НайтиЦикл(i0, j0)

   гл_сч = m*n;
   iЦикл.Очистить();
   jЦикл.Очистить();
   Если НайтиЦикл_ПоГоризонтали(i0, j0) Тогда
       Возврат Истина;
   КонецЕсли;
   Возврат Ложь;

КонецФункции

Функция НайтиЦикл_ПоГоризонтали(i0, j0)

   гл_сч=гл_сч-1;
   Если гл_сч=0 Тогда
       ВызватьИсключение("Too many iterations in the cycle search");
   КонецЕсли;    
   Для j=1 по n Цикл
       Если j=j0 Тогда
           Продолжить;
       КонецЕсли;
       Если БазисныеЯчейки[i0][j]=0 Тогда
           Продолжить;
       КонецЕсли;
       Если НайтиЦикл_ПоВертикали(i0, j) Тогда
           iЦикл.Добавить(i0);
           jЦикл.Добавить(j);
           Возврат Истина;
       КонецЕсли;    
   КонецЦикла;
   Возврат Ложь;

КонецФункции

Функция НайтиЦикл_ПоВертикали(i0, j0)

   Для i=1 по m Цикл
       Если (j0=j1) и (i=i1) Тогда
               iЦикл.Добавить(i);
               jЦикл.Добавить(j0);
               Возврат Истина;
       КонецЕсли;    
       Если i=i0 Тогда
           Продолжить;
       КонецЕсли;
       Если БазисныеЯчейки[i][j0]=0 Тогда
           Продолжить;
       КонецЕсли;
       Если НайтиЦикл_ПоГоризонтали(i, j0) Тогда
           iЦикл.Добавить(i);
           jЦикл.Добавить(j0);
           Возврат Истина;
       КонецЕсли;    
   КонецЦикла;    
   Возврат Ложь;

КонецФункции

Функция ПерераспределениеПоЦиклу()

   Сообщить("Redistribution by the cycle "+iЦикл.Количество());
   Если jЦикл.Количество()<>iЦикл.Количество() Тогда
       ВызватьИсключение("Unequal dimension for the cycle coordinates");
   КонецЕсли;
   Если iЦикл.Количество()<4 Тогда
       ВызватьИсключение("Cycle is less than 4 items");
   КонецЕсли;    
   Тета=НеОпределено;
   Знак="+";
   Для й=0 по iЦикл.ВГраница() Цикл
       i=iЦикл[й];
       j=jЦикл[й];
       Если Знак="-" Тогда
           Объем=Отгрузки[i][j];
           Если Тета=НеОпределено Тогда
               Тета=Объем;
           Иначе
               Если Объем<Тета Тогда
                   Тета=Объем;
               КонецЕсли;    
           КонецЕсли;    
           Знак="+";
       Иначе
           Знак="-";
       КонецЕсли;    
   КонецЦикла;    
   Если Тета=НеОпределено Тогда
       ВызватьИсключение("Failed to evaluate variable theta.");
   КонецЕсли;
   Сообщить("Тета="+Тета);
   Если Тета=0 Тогда
       Возврат Ложь;
   КонецЕсли;
   Знак="+";
   Для й=0 по iЦикл.ВГраница() Цикл
       i=iЦикл[й];
       j=jЦикл[й];
       Если Знак="-" Тогда
           Отгрузки[i][j]=Отгрузки[i][j]-Тета;
           Знак="+";
       Иначе
           Отгрузки[i][j]=Отгрузки[i][j]+Тета;
           Знак="-";
       КонецЕсли;    
   КонецЦикла;
   Возврат Истина;

КонецФункции

Функция РешениеТранспортнойЗадачи()

   ГСЧ = Новый ГенераторСлучайныхЧисел();
   БазисныеЯчейки = Новый Массив(m+1,n+1);
   Отгрузки = Новый Массив(m+1,n+1);
   СпросОстаток=Новый Массив(n+1);
   ПредложениеОстаток=Новый Массив(m+1);
   u=Новый Массив(m+1);
   v=Новый Массив(n+1);
   iЦикл = Новый Массив;
   jЦикл = Новый Массив;
   чСпрос=0;
   Для j=1 по n Цикл
       чСпрос=чСпрос+Спрос[j];
   КонецЦикла;    
   чПредложение=0;
   Для i=1 по m Цикл
       чПредложение=чПредложение+Предложение[i];
   КонецЦикла;
   Если чПредложение>чСпрос Тогда
       Сообщить("Offering more than the demand for "+(чПредложение-чСпрос)+" units of cargo. Create a fictitious user.");
       Возврат Ложь;
   ИначеЕсли чПредложение<чСпрос Тогда
       Сообщить("Offering less than the demand for "+(чСпрос-чПредложение)+" units of cargo. Create a fictitious vendor.");
       Возврат Ложь;
   КонецЕсли;        
   РаспределениеМетодомСевероЗападногоУгла();
   чСумма=СтоимостьПеревозки();
   Сообщить("The cost of transportation by the north-west corner: "+чСумма);
   Пока 1=1 Цикл
       ПроверкаПравильностиОтгрузок();
       счБазисных=0;
       Для i=1 по m Цикл
           Для j=1 по n Цикл
               Если Отгрузки[i][j]>0 Тогда
                   БазисныеЯчейки[i][j]=1;
                   счБазисных=счБазисных+1;
               ИначеЕсли Отгрузки[i][j]<0 Тогда    
                   ВызватьИсключение("Shipments should not be negative");
               Иначе
                   БазисныеЯчейки[i][j]=0;
               КонецЕсли;    
           КонецЦикла;    
       КонецЦикла;
       Пока счБазисных<(m+n-1) Цикл
           Сообщить("Решение вырождено");
           ПоискНулевойЯчейкиДляВводаВБазис();
           счБазисных=счБазисных+1;
       КонецЦикла;
       Если ВычислениеПотенциалов()=Ложь Тогда
           Продолжить;
       КонецЕсли;    
       Если ПроверкаОптимальности()=Истина Тогда
           Сообщить("Solution is optimal.");
           Прервать;
       КонецЕсли;
       Сообщить("Solution is not optimal.");
       Если НайтиЦикл(i1, j1)= Ложь Тогда
           ВызватьИсключение("Unable to find a cycle");
       КонецЕсли;
       ПерераспределениеПоЦиклу();
       чСумма=СтоимостьПеревозки();
       Сообщить("***");
       Сообщить("The cost of transport: "+чСумма);
   КонецЦикла;    
   Возврат Истина;

КонецФункции

&НаКлиенте Процедура КомандаРассчитать(Команда)

   РешениеТранспортнойЗадачи();

КонецПроцедуры</lang>

C#

Translation of: Java

<lang csharp>using System; using System.Collections.Generic; using System.IO; using System.Linq;

namespace TransportationProblem {

   class Shipment {
       public Shipment(double q, double cpu, int r, int c) {
           Quantity = q;
           CostPerUnit = cpu;
           R = r;
           C = c;
       }
       public double CostPerUnit { get; }
       public double Quantity { get; set; }
       public int R { get; }
       public int C { get; }
   }
   class Program {
       private static int[] demand;
       private static int[] supply;
       private static double[,] costs;
       private static Shipment[,] matrix;
       static void Init(string filename) {
           string line;
           using (StreamReader file = new StreamReader(filename)) {
               line = file.ReadLine();
               var numArr = line.Split();
               int numSources = int.Parse(numArr[0]);
               int numDestinations = int.Parse(numArr[1]);
               List<int> src = new List<int>();
               List<int> dst = new List<int>();
               line = file.ReadLine();
               numArr = line.Split();
               for (int i = 0; i < numSources; i++) {
                   src.Add(int.Parse(numArr[i]));
               }
               line = file.ReadLine();
               numArr = line.Split();
               for (int i = 0; i < numDestinations; i++) {
                   dst.Add(int.Parse(numArr[i]));
               }
               // fix imbalance
               int totalSrc = src.Sum();
               int totalDst = dst.Sum();
               if (totalSrc > totalDst) {
                   dst.Add(totalSrc - totalDst);
               } else if (totalDst > totalSrc) {
                   src.Add(totalDst - totalSrc);
               }
               supply = src.ToArray();
               demand = dst.ToArray();
               costs = new double[supply.Length, demand.Length];
               matrix = new Shipment[supply.Length, demand.Length];
               for (int i = 0; i < numSources; i++) {
                   line = file.ReadLine();
                   numArr = line.Split();
                   for (int j = 0; j < numDestinations; j++) {
                       costs[i, j] = int.Parse(numArr[j]);
                   }
               }
           }
       }
       static void NorthWestCornerRule() {
           for (int r = 0, northwest = 0; r < supply.Length; r++) {
               for (int c = northwest; c < demand.Length; c++) {
                   int quantity = Math.Min(supply[r], demand[c]);
                   if (quantity > 0) {
                       matrix[r, c] = new Shipment(quantity, costs[r, c], r, c);
                       supply[r] -= quantity;
                       demand[c] -= quantity;
                       if (supply[r] == 0) {
                           northwest = c;
                           break;
                       }
                   }
               }
           }
       }
       static void SteppingStone() {
           double maxReduction = 0;
           Shipment[] move = null;
           Shipment leaving = null;
           FixDegenerateCase();
           for (int r = 0; r < supply.Length; r++) {
               for (int c = 0; c < demand.Length; c++) {
                   if (matrix[r, c] != null) {
                       continue;
                   }
                   Shipment trial = new Shipment(0, costs[r, c], r, c);
                   Shipment[] path = GetClosedPath(trial);
                   double reduction = 0;
                   double lowestQuantity = int.MaxValue;
                   Shipment leavingCandidate = null;
                   bool plus = true;
                   foreach (var s in path) {
                       if (plus) {
                           reduction += s.CostPerUnit;
                       } else {
                           reduction -= s.CostPerUnit;
                           if (s.Quantity < lowestQuantity) {
                               leavingCandidate = s;
                               lowestQuantity = s.Quantity;
                           }
                       }
                       plus = !plus;
                   }
                   if (reduction < maxReduction) {
                       move = path;
                       leaving = leavingCandidate;
                       maxReduction = reduction;
                   }
               }
           }
           if (move != null) {
               double q = leaving.Quantity;
               bool plus = true;
               foreach (var s in move) {
                   s.Quantity += plus ? q : -q;
                   matrix[s.R, s.C] = s.Quantity == 0 ? null : s;
                   plus = !plus;
               }
               SteppingStone();
           }
       }
       static List<Shipment> MatrixToList() {
           List<Shipment> newList = new List<Shipment>();
           foreach (var item in matrix) {
               if (null != item) {
                   newList.Add(item);
               }
           }
           return newList;
       }
       static Shipment[] GetClosedPath(Shipment s) {
           List<Shipment> path = MatrixToList();
           path.Add(s);
           // remove (and keep removing) elements that do not have a
           // vertical AND horizontal neighbor
           int before;
           do {
               before = path.Count;
               path.RemoveAll(ship => {
                   var nbrs = GetNeighbors(ship, path);
                   return nbrs[0] == null || nbrs[1] == null;
               });
           } while (before != path.Count);
           // place the remaining elements in the correct plus-minus order
           Shipment[] stones = path.ToArray();
           Shipment prev = s;
           for (int i = 0; i < stones.Length; i++) {
               stones[i] = prev;
               prev = GetNeighbors(prev, path)[i % 2];
           }
           return stones;
       }
       static Shipment[] GetNeighbors(Shipment s, List<Shipment> lst) {
           Shipment[] nbrs = new Shipment[2];
           foreach (var o in lst) {
               if (o != s) {
                   if (o.R == s.R && nbrs[0] == null) {
                       nbrs[0] = o;
                   } else if (o.C == s.C && nbrs[1] == null) {
                       nbrs[1] = o;
                   }
                   if (nbrs[0] != null && nbrs[1] != null) {
                       break;
                   }
               }
           }
           return nbrs;
       }
       static void FixDegenerateCase() {
           const double eps = double.Epsilon;
           if (supply.Length + demand.Length - 1 != MatrixToList().Count) {
               for (int r = 0; r < supply.Length; r++) {
                   for (int c = 0; c < demand.Length; c++) {
                       if (matrix[r, c] == null) {
                           Shipment dummy = new Shipment(eps, costs[r, c], r, c);
                           if (GetClosedPath(dummy).Length == 0) {
                               matrix[r, c] = dummy;
                               return;
                           }
                       }
                   }
               }
           }
       }
       
       static void PrintResult(string filename) {
           Console.WriteLine("Optimal solution {0}\n", filename);
           double totalCosts = 0;
           for (int r = 0; r < supply.Length; r++) {
               for (int c = 0; c < demand.Length; c++) {
                   Shipment s = matrix[r, c];
                   if (s != null && s.R == r && s.C == c) {
                       Console.Write(" {0,3} ", s.Quantity);
                       totalCosts += (s.Quantity * s.CostPerUnit);
                   } else {
                       Console.Write("  -  ");
                   }
               }
               Console.WriteLine();
           }
           Console.WriteLine("\nTotal costs: {0}\n", totalCosts);
       }
       static void Main() {
           foreach (var filename in new string[] { "input1.txt", "input2.txt", "input3.txt" }) {
               Init(filename);
               NorthWestCornerRule();
               SteppingStone();
               PrintResult(filename);
           }
       }
   }

}</lang>

Output:
Optimal solution input1.txt

  20   -     5
  -    30    5

Total costs: 180

Optimal solution input2.txt

  -    -    -    12
  20   -    10   10
  -    30   -     3

Total costs: 130

Optimal solution input3.txt

  -    -    -    14
  -     9   -     1
  10   -     5   -
  -     5    7   -
  -     1   -    -

Total costs: 1000

C++

Translation of: Kotlin

<lang cpp>#include <algorithm>

  1. include <iomanip>
  2. include <iostream>
  3. include <fstream>
  4. include <numeric>
  5. include <string>
  6. include <vector>

using namespace std;

class Shipment { public:

   double costPerUnit;
   int r, c;
   double quantity;
   Shipment() : quantity(0), costPerUnit(0), r(-1), c(-1) {
       // empty
   }
   Shipment(double q, double cpu, int r, int c) : quantity(q), costPerUnit(cpu), r(r), c(c) {
       // empty
   }
   friend bool operator==(const Shipment &lhs, const Shipment &rhs) {
       return lhs.costPerUnit == rhs.costPerUnit
           && lhs.quantity == rhs.quantity
           && lhs.r == rhs.r
           && lhs.c == rhs.c;
   }
   friend bool operator!=(const Shipment &lhs, const Shipment &rhs) {
       return !(lhs == rhs);
   }
   static Shipment ZERO;

}; Shipment Shipment::ZERO = {};

vector<int> demand, supply; vector<vector<double>> costs; vector<vector<Shipment>> matrix;

void init(string filename) {

   ifstream ifs;
   ifs.open(filename);
   if (!ifs) {
       cerr << "File not found: " << filename << '\n';
       return;
   }
   size_t numSources, numDestinations;
   ifs >> numSources >> numDestinations;
   vector<int> src, dst;
   int t;
   for (size_t i = 0; i < numSources; i++) {
       ifs >> t;
       src.push_back(t);
   }
   for (size_t i = 0; i < numDestinations; i++) {
       ifs >> t;
       dst.push_back(t);
   }
   // fix imbalance
   int totalSrc = accumulate(src.cbegin(), src.cend(), 0);
   int totalDst = accumulate(dst.cbegin(), dst.cend(), 0);
   if (totalSrc > totalDst) {
       dst.push_back(totalSrc - totalDst);
   } else if (totalDst > totalSrc) {
       src.push_back(totalDst - totalSrc);
   }
   supply = src;
   demand = dst;
   costs.clear();
   matrix.clear();
   double d;
   for (size_t i = 0; i < numSources; i++) {
       size_t cap = max(numDestinations, demand.size());
       vector<double> dt(cap);
       vector<Shipment> st(cap);
       for (size_t j = 0; j < numDestinations; j++) {
           ifs >> d;
           dt[j] = d;
       }
       costs.push_back(dt);
       matrix.push_back(st);
   }
   for (size_t i = numSources; i < supply.size(); i++) {
       size_t cap = max(numDestinations, demand.size());
       vector<Shipment> st(cap);
       matrix.push_back(st);
       vector<double> dt(cap);
       costs.push_back(dt);
   }

}

void northWestCornerRule() {

   int northwest = 0;
   for (size_t r = 0; r < supply.size(); r++) {
       for (size_t c = northwest; c < demand.size(); c++) {
           int quantity = min(supply[r], demand[c]);
           if (quantity > 0) {
               matrix[r][c] = Shipment(quantity, costs[r][c], r, c);
               supply[r] -= quantity;
               demand[c] -= quantity;
               if (supply[r] == 0) {
                   northwest = c;
                   break;
               }
           }
       }
   }

}

vector<Shipment> matrixToVector() {

   vector<Shipment> result;
   for (auto &row : matrix) {
       for (auto &s : row) {
           if (s != Shipment::ZERO) {
               result.push_back(s);
           }
       }
   }
   return result;

}

vector<Shipment> getNeighbors(const Shipment &s, const vector<Shipment> &lst) {

   vector<Shipment> nbrs(2);
   for (auto &o : lst) {
       if (o != s) {
           if (o.r == s.r && nbrs[0] == Shipment::ZERO) {
               nbrs[0] = o;
           } else if (o.c == s.c && nbrs[1] == Shipment::ZERO) {
               nbrs[1] = o;
           }
           if (nbrs[0] != Shipment::ZERO && nbrs[1] != Shipment::ZERO) {
               break;
           }
       }
   }
   return nbrs;

}

vector<Shipment> getClosedPath(const Shipment &s) {

   auto path = matrixToVector();
   path.insert(path.begin(), s);
   // remove (and keep removing) elements that do not have a
   // vertical AND horizontal neighbor
   size_t before;
   do {
       before = path.size();
       path.erase(
           remove_if(
               path.begin(), path.end(),
               [&path](Shipment &ship) {
                   auto nbrs = getNeighbors(ship, path);
                   return nbrs[0] == Shipment::ZERO || nbrs[1] == Shipment::ZERO;
               }),
           path.end());
   } while (before != path.size());
   // place the remaining elements in the correct plus-minus order
   vector<Shipment> stones(path.size());
   fill(stones.begin(), stones.end(), Shipment::ZERO);
   auto prev = s;
   for (size_t i = 0; i < stones.size(); i++) {
       stones[i] = prev;
       prev = getNeighbors(prev, path)[i % 2];
   }
   return stones;

}

void fixDegenerateCase() {

   double eps = DBL_MIN;
   if (supply.size() + demand.size() - 1 != matrixToVector().size()) {
       for (size_t r = 0; r < supply.size(); r++) {
           for (size_t c = 0; c < demand.size(); c++) {
               if (matrix[r][c] == Shipment::ZERO) {
                   Shipment dummy(eps, costs[r][c], r, c);
                   if (getClosedPath(dummy).empty()) {
                       matrix[r][c] = dummy;
                       return;
                   }
               }
           }
       }
   }

}

void steppingStone() {

   double maxReduction = 0;
   vector<Shipment> move;
   Shipment leaving;
   bool isNull = true;
   fixDegenerateCase();
   for (size_t r = 0; r < supply.size(); r++) {
       for (size_t c = 0; c < demand.size(); c++) {
           if (matrix[r][c] != Shipment::ZERO) {
               continue;
           }
           Shipment trial(0, costs[r][c], r, c);
           vector<Shipment> path = getClosedPath(trial);
           double reduction = 0;
           double lowestQuantity = INT32_MAX;
           Shipment leavingCandidate;
           bool plus = true;
           for (auto &s : path) {
               if (plus) {
                   reduction += s.costPerUnit;
               } else {
                   reduction -= s.costPerUnit;
                   if (s.quantity < lowestQuantity) {
                       leavingCandidate = s;
                       lowestQuantity = s.quantity;
                   }
               }
               plus = !plus;
           }
           if (reduction < maxReduction) {
               isNull = false;
               move = path;
               leaving = leavingCandidate;
               maxReduction = reduction;
           }
       }
   }
   if (!isNull) {
       double q = leaving.quantity;
       bool plus = true;
       for (auto &s : move) {
           s.quantity += plus ? q : -q;
           matrix[s.r][s.c] = s.quantity == 0 ? Shipment::ZERO : s;
           plus = !plus;
       }
       steppingStone();
   }

}

void printResult(string filename) {

   ifstream ifs;
   string buffer;
   ifs.open(filename);
   if (!ifs) {
       cerr << "File not found: " << filename << '\n';
       return;
   }
   cout << filename << "\n\n";
   while (!ifs.eof()) {
       getline(ifs, buffer);
       cout << buffer << '\n';
   }
   cout << '\n';
   cout << "Optimal solution " << filename << "\n\n";
   double totalCosts = 0.0;
   for (size_t r = 0; r < supply.size(); r++) {
       for (size_t c = 0; c < demand.size(); c++) {
           auto s = matrix[r][c];
           if (s != Shipment::ZERO && s.r == r && s.c == c) {
               cout << ' ' << setw(3) << s.quantity << ' ';
               totalCosts += s.quantity * s.costPerUnit;
           } else {
               cout << "  -  ";
           }
       }
       cout << '\n';
   }
   cout << "\nTotal costs: " << totalCosts << "\n\n";

}

void process(string filename) {

   init(filename);
   northWestCornerRule();
   steppingStone();
   printResult(filename);

}

int main() {

   process("input1.txt");
   process("input2.txt");
   process("input3.txt");
   return 0;

}</lang>

Output:
input1.txt

2 3
25 35
20 30 10
3 5 7
3 2 5

Optimal solution input1.txt

  20   -     5
  -    30    5

Total costs: 180

input2.txt

3 3
12 40 33
20 30 10
3 5 7
2 4 6
9 1 8

Optimal solution input2.txt

  -    -    -    12
  20   -    10   10
  -    30   -     3

Total costs: 130

input3.txt

4 4
14 10 15 12
10 15 12 15
10 30 25 15
20 15 20 10
10 30 20 20
30 40 35 45

Optimal solution input3.txt

  -    -    -    14
  -     9   -     1
  10   -     5   -
  -     5    7   -
  -     1   -    -

Total costs: 1000

D

Translation of: Java

<lang d>import std.stdio, std.range, std.algorithm, std.conv, std.math, std.traits;

final class Shipment {

   double quantity;
   immutable double costPerUnit;
   immutable size_t r, c;
   this(in double q, in double cpu, in size_t r_, in size_t c_)
   pure nothrow @safe @nogc {
       quantity = q;
       costPerUnit = cpu;
       this.r = r_;
       this.c = c_;
   }

}

alias ShipmentMat = Shipment[][]; alias CostsMat = double[][];

void init(in string fileName, out uint[] demand, out uint[] supply,

         out CostsMat costs, out ShipmentMat matrix) {
   auto inParts = fileName.File.byLine.map!splitter.joiner;
   immutable numSources = inParts.front.to!uint;
   inParts.popFront;
   immutable numDestinations = inParts.front.to!uint;
   inParts.popFront;
   foreach (immutable i; 0 .. numSources) {
       supply ~= inParts.front.to!uint;
       inParts.popFront;
   }
   foreach (immutable i; 0 .. numDestinations) {
       demand ~= inParts.front.to!uint;
       inParts.popFront;
   }
   // Fix imbalance.
   immutable totalSrc = supply.sum;
   immutable totalDst = demand.sum;
   if (totalSrc > totalDst)
       demand ~= totalSrc - totalDst;
   else if (totalDst > totalSrc)
       supply ~= totalDst - totalSrc;
   costs = new CostsMat(supply.length, demand.length);
   foreach (row; costs)
       row[] = 0.0;
   matrix = new ShipmentMat(supply.length, demand.length);
   foreach (immutable i; 0 .. numSources)
       foreach (immutable j; 0 .. numDestinations) {
           costs[i][j] = inParts.front.to!double;
           inParts.popFront;
       }

}

void northWestCornerRule(uint[] demand, uint[] supply, in CostsMat costs,

                        ShipmentMat matrix) pure nothrow @safe {
   size_t northwest = 0;
   foreach (immutable r; 0 .. supply.length) {
       foreach (immutable c; northwest .. demand.length) {
           immutable quantity = min(supply[r], demand[c]);
           if (quantity > 0) {
               matrix[r][c] = new Shipment(quantity, costs[r][c], r, c);
               supply[r] -= quantity;
               demand[c] -= quantity;
               if (supply[r] == 0) {
                   northwest = c;
                   break;
               }
           }
       }
   }

}

void steppingStone(in uint[] demand, in uint[] supply,

                  in CostsMat costs, ShipmentMat matrix) pure @safe {
   double maxReduction = 0;
   Shipment[] move;
   Shipment leaving = null;
   fixDegenerateCase(demand, supply, costs, matrix);
   foreach (immutable r; 0 .. supply.length) {
       foreach (immutable c; 0 .. demand.length) {
           if (matrix[r][c] !is null)
               continue;
           auto trial = new Shipment(0, costs[r][c], r, c);
           auto path = getClosedPath(trial, matrix);
           double reduction = 0;
           double lowestQuantity = uint.max;
           Shipment leavingCandidate = null;
           bool plus = true;
           foreach (s; path) {
               if (plus) {
                   reduction += s.costPerUnit;
               } else {
                   reduction -= s.costPerUnit;
                   if (s.quantity < lowestQuantity) {
                       leavingCandidate = s;
                       lowestQuantity = s.quantity;
                   }
               }
               plus = !plus;
           }
           if (reduction < maxReduction) {
               move = path;
               leaving = leavingCandidate;
               maxReduction = reduction;
           }
       }
   }
   if (move !is null) {
       auto q = leaving.quantity;
       auto plus = true;
       foreach (s; move) {
           s.quantity += plus ? q : -q;
           matrix[s.r][s.c] = (s.quantity == 0) ? null : s;
           plus = !plus;
       }
       steppingStone(demand, supply, costs, matrix);
   }

}

auto matrixToSeq(ShipmentMat matrix) pure nothrow @nogc @safe {

   return matrix.joiner.filter!(s => s !is null);

}

Shipment[] getClosedPath(Shipment s, ShipmentMat matrix) pure @safe in {

   assert(s !is null);

} out(result) {

   assert(result.all!(sh => sh !is null));

} body {

   Shipment[] stones = chain([s], matrixToSeq(matrix)).array;
   // Remove (and keep removing) elements that do not have
   // a vertical AND horizontal neighbor.
   while (true) {
       auto stones2 = stones.remove!((in e) {
           const nbrs = getNeighbors(e, stones);
           return nbrs[0] is null || nbrs[1] is null;
       });
       if (stones2.length == stones.length)
           break;
       stones = stones2;
   }
   // Place the remaining elements in the correct plus-minus order.
   auto stones3 = stones.dup;
   Shipment prev = s;
   foreach (immutable i, ref si; stones3) {
       si = prev;
       prev = getNeighbors(prev, stones)[i % 2];
   }
   return stones3;

}

Shipment[2] getNeighbors(ShipmentsRange)(in Shipment s, ShipmentsRange seq) pure nothrow @safe @nogc if (isForwardRange!ShipmentsRange && is(ForeachType!ShipmentsRange == Shipment)) in {

   assert(s !is null);
   assert(seq.all!(sh => sh !is null));

} body {

   Shipment[2] nbrs;
   foreach (o; seq) {
       if (o !is s) {
           if (o.r == s.r && nbrs[0] is null)
               nbrs[0] = o;
           else if (o.c == s.c && nbrs[1] is null)
               nbrs[1] = o;
           if (nbrs[0] !is null && nbrs[1] !is null)
               break;
       }
   }
   return nbrs;

}

void fixDegenerateCase(in uint[] demand, in uint[] supply,

                      in CostsMat costs, ShipmentMat matrix) pure @safe {
   immutable eps = double.min_normal;
   if (supply.length.signed + demand.length.signed - 1 != matrixToSeq(matrix).walkLength) {
       foreach (immutable r; 0 .. supply.length) {
           foreach (immutable c; 0 .. demand.length) {
               if (matrix[r][c] is null) {
                   auto dummy = new Shipment(eps, costs[r][c], r, c);
                   if (getClosedPath(dummy, matrix).length == 0) {
                       matrix[r][c] = dummy;
                       return;
                   }
               }
           }
       }
   }

}

void printResult(in string fileName, in uint[] demand, in uint[] supply,

                in CostsMat costs, in ShipmentMat matrix) @safe /*@nogc*/ {
   writefln("Optimal solution %s", fileName);
   double totalCosts = 0;
   foreach (immutable r; 0 .. supply.length) {
       foreach (immutable c; 0 .. demand.length) {
           const s = matrix[r][c];
           if (s !is null && s.r == r && s.c == c) {
               writef(" %3d ", cast(uint)s.quantity);
               totalCosts += s.quantity * s.costPerUnit;
           } else
               write("  -  ");
       }
       //writeln; // Not @safe?
       write('\n');
   }
   writefln("\nTotal costs: %s\n", totalCosts);

}

void main() {

   foreach (fileName; ["transportation_problem1.txt",
                       "transportation_problem2.txt",
                       "transportation_problem3.txt"]) {
       uint[] demand, supply;
       CostsMat costs;
       ShipmentMat matrix;
       init(fileName, demand, supply, costs, matrix);
       northWestCornerRule(demand, supply, costs, matrix);
       steppingStone(demand, supply, costs, matrix);
       printResult(fileName, demand, supply, costs, matrix);
   }

}</lang>

Output:
Optimal solution transportation_problem1.txt
  20   -     5 
  -    30    5 

Total costs: 180

Optimal solution transportation_problem2.txt
  -    -    -    12 
  20   -    10   10 
  -    30   -     3 

Total costs: 130

Optimal solution transportation_problem3.txt
  -    -    -    14 
  -     9   -     1 
  10   -     5   -  
  -     5    7   -  
  -     1   -    -  

Total costs: 1000

Glagol

<lang>ОТДЕЛ Транспорт+; ИСПОЛЬЗУЕТ

 Вывод ИЗ "...\Отделы\Обмен\",
 Приём;

ПЕР

 Поставщиков, Потребителей: ЦЕЛ;
 Запасы, Потребности: ДОСТУП К РЯД ИЗ ЦЕЛ;
 Расходы, План: ДОСТУП К РЯД ИЗ РЯД ИЗ ЦЕЛ;
 U, V: ДОСТУП К РЯД ИЗ ЦЕЛ;
 оцСв: ДОСТУП К РЯД ИЗ НАБОР значение: ЦЕЛ; поставщик, потребитель: ЦЕЛ КОН;
 начQ_поставщик, начQ_потребитель: ЦЕЛ;
 Q: ДОСТУП К РЯД ИЗ РЯД ИЗ УЗКЦЕЛ;
 Поправка, Разница: ЦЕЛ;

ЗАДАЧА ПринятьДанные; ПЕР

 сч1, сч2: ЦЕЛ;
 сумма1, сумма2, разница: ЦЕЛ;
 памЗап, памПотр: ДОСТУП К РЯД ИЗ ЦЕЛ;

УКАЗ

 Вывод.Цепь("Number of suppliers: ");
 Поставщиков := Приём.Число();
 Вывод.Цепь(".^Number of consumers: ");
 Потребителей := Приём.Число();
 СОЗДАТЬ(памЗап, Поставщиков);
 СОЗДАТЬ(памПотр, Потребителей);
 Вывод.Цепь(".^Inventories^^");
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
   памЗап[сч1] := Приём.Число();
   Вывод.Цепь(" ")
 КОН;
 Вывод.Цепь("^Requirements:^");
 ОТ сч1 := 0 ДО Потребителей-1 ВЫП
   памПотр[сч1] := Приём.Число();
   Вывод.Цепь(" ")
 КОН;
 сумма1 := 0;
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП УВЕЛИЧИТЬ(сумма1, памЗап[сч1]) КОН;
 сумма2 := 0;
 ОТ сч1 := 0 ДО Потребителей-1 ВЫП УВЕЛИЧИТЬ(сумма2, памПотр[сч1]) КОН;
 ЕСЛИ сумма1 > сумма2 ТО
   разница := сумма1 - сумма2;
   Вывод.ЧЦел("^Introduced a fictitious consumer.", сумма1, сумма2, разница, 0);
   УВЕЛИЧИТЬ(Потребителей);
   СОЗДАТЬ(Потребности, Потребителей);
   ОТ сч1 := 0 ДО Потребителей-2 ВЫП Потребности[сч1] := памПотр[сч1] КОН;
   Потребности[Потребителей-1] := разница;
   СОЗДАТЬ(Запасы, Поставщиков);
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП Запасы[сч1] := памЗап[сч1] КОН
 АЕСЛИ сумма2 > сумма1 ТО
   разница := сумма2 - сумма1;
   Вывод.ЧЦел("^Introduced a fictitious supplier.", сумма2, сумма1, разница, 0);
   УВЕЛИЧИТЬ(Поставщиков);
   СОЗДАТЬ(Запасы, Поставщиков);
   ОТ сч1 := 0 ДО Поставщиков-2 ВЫП Запасы[сч1] := памЗап[сч1] КОН;
   Запасы[Поставщиков-1] := разница;
   СОЗДАТЬ(Потребности, Потребителей);
   ОТ сч1 := 0 ДО Потребителей-1 ВЫП Потребности[сч1] := памПотр[сч1] КОН
 ИНАЧЕ
   СОЗДАТЬ(Запасы, Поставщиков);
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП Запасы[сч1] := памЗап[сч1] КОН;
   СОЗДАТЬ(Потребности, Потребителей);
   ОТ сч1 := 0 ДО Потребителей-1 ВЫП Потребности[сч1] := памПотр[сч1] КОН
 КОН;
 СОЗДАТЬ(Расходы, Поставщиков, Потребителей);
 Вывод.Цепь("^The matrix of costs:^");
 ЕСЛИ сумма1 > сумма2 ТО
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
     ОТ сч2 := 0 ДО Потребителей-2 ВЫП
       Расходы[сч1, сч2] := Приём.Число();
       Вывод.Цепь(" ")
     КОН;
     Расходы[сч1, Потребителей-1] := 0;
     Вывод.Цепь("^")
   КОН
 АЕСЛИ сумма2 > сумма1 ТО
   ОТ сч1 := 0 ДО Поставщиков-2 ВЫП
     ОТ сч2 := 0 ДО Потребителей-1 ВЫП
       Расходы[сч1, сч2] := Приём.Число();
       Вывод.Цепь(" ")
     КОН;
     Вывод.Цепь("^")
   КОН;
   ОТ сч1 := 0 ДО Потребителей-1 ВЫП Расходы[Поставщиков-1, сч1] := 0 КОН
 ИНАЧЕ
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
     ОТ сч2 := 0 ДО Потребителей-1 ВЫП
       Расходы[сч1, сч2] := Приём.Число();
       Вывод.Цепь(" ")
     КОН;
     Вывод.Цепь("^")
   КОН
 КОН;
 СОЗДАТЬ(План, Поставщиков, Потребителей);
 СОЗДАТЬ(U, Поставщиков);
 СОЗДАТЬ(V, Потребителей);
 СОЗДАТЬ(оцСв, Потребителей*Поставщиков-(Потребителей+Поставщиков-1));
 СОЗДАТЬ(Q, Поставщиков, Потребителей)

КОН ПринятьДанные;

ЗАДАЧА ВывестиПлан; ПЕР

 сч1, сч2: ЦЕЛ;

УКАЗ

 ОТ сч1 := 1 ДО Потребителей ВЫП Вывод.Цепь("-----") КОН;
 Вывод.Цепь("^");
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
   ОТ сч2 := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ План[сч1, сч2] = -1 ТО Вывод.Цепь("  -  ") ИНАЧЕ
       Вывод.ЧЦел("%4d ", План[сч1, сч2], 0, 0, 0);
     КОН
   КОН;
   Вывод.Цепь("^")
 КОН;
 ОТ сч1 := 1 ДО Потребителей ВЫП Вывод.Цепь("-----") КОН

КОН ВывестиПлан;

ЗАДАЧА ПосчитатьПоправку; ПЕР

 сч1, сч2: ЦЕЛ;

УКАЗ

 Поправка := -1;
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
   ОТ сч2 := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ План[сч1, сч2] # -1 ТО
       ЕСЛИ Q[сч1, сч2] = -1 ТО
         ЕСЛИ Поправка = -1 ТО Поправка := План[сч1, сч2]
         АЕСЛИ Поправка > План[сч1, сч2] ТО Поправка := План[сч1, сч2] КОН
       КОН
     КОН
   КОН
 КОН;
 Разница := Разница * Поправка

КОН ПосчитатьПоправку;

ЗАДАЧА РасставитьНули(недостаток: ЦЕЛ); ПЕР

 Связь: ДОСТУП К РЯД ИЗ РЯД ИЗ УЗКЦЕЛ;
 сч1, сч2: ЦЕЛ;
 естьБезСвязи: КЛЮЧ;
 ЗАДАЧА ЕстьНапротив(строка, столбец: ЦЕЛ): КЛЮЧ;
 ПЕР сч: ЦЕЛ;
 УКАЗ
   ОТ сч := 0 ДО Поставщиков-1 ВЫП
     ЕСЛИ (сч # строка) И (Связь[сч, столбец] = 1) ТО ВОЗВРАТ ВКЛ КОН
   КОН;
   ОТ сч := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ (сч # столбец) И (Связь[строка, сч] = 1) ТО ВОЗВРАТ ВКЛ КОН
   КОН;
   ВОЗВРАТ ОТКЛ
 КОН ЕстьНапротив;
 ЗАДАЧА СтолбецБезСвязи(номер: ЦЕЛ): КЛЮЧ;
 ПЕР сч: ЦЕЛ;
 УКАЗ
   ОТ сч := 0 ДО Поставщиков-1 ВЫП
     ЕСЛИ Связь[сч, номер] = 1 ТО ВОЗВРАТ ОТКЛ КОН
   КОН;
   ВОЗВРАТ ВКЛ
 КОН СтолбецБезСвязи;

УКАЗ

 СОЗДАТЬ(Связь, Поставщиков, Потребителей);
 естьБезСвязи := ОТКЛ;
 ОТ сч1 := 0 ДО Потребителей-1 ВЫП
   ЕСЛИ План[0, сч1] = -1 ТО Связь[0, сч1] := -1 ИНАЧЕ Связь[0, сч1] := 1 КОН
 КОН;
 ОТ сч1 := 1 ДО Поставщиков-1 ВЫП
   ОТ сч2 := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ План[сч1, сч2] = -1 ТО Связь[сч1, сч2] := -1 ИНАЧЕ Связь[сч1, сч2] := 0 КОН
   КОН
 КОН;
 ОТ сч1 := 1 ДО Поставщиков-1 ВЫП
   ОТ сч2 := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ Связь[сч1, сч2] = 0 ТО
       ЕСЛИ ЕстьНапротив(сч1, сч2) ТО Связь[сч1, сч2] := 1
       АЕСЛИ НЕ естьБезСвязи ТО естьБезСвязи := ВКЛ КОН
     КОН
   КОН
 КОН;
 ЕСЛИ естьБезСвязи ТО
   ОТ сч1 := Поставщиков-1 ДО 1 ПО -1 ВЫП
     ОТ сч2 := Потребителей-1 ДО 0 ПО -1 ВЫП
       ЕСЛИ Связь[сч1, сч2] = 0 ТО
         ЕСЛИ ЕстьНапротив(сч1, сч2) ТО Связь[сч1, сч2] := 1
         АЕСЛИ НЕ естьБезСвязи ТО естьБезСвязи := ВКЛ КОН
       КОН
     КОН
   КОН
 КОН;
 ЕСЛИ естьБезСвязи ТО
   ОТ сч1 := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ СтолбецБезСвязи(сч1) ТО План[0, сч1] := 0; УМЕНЬШИТЬ(недостаток) КОН;
     ЕСЛИ недостаток = 0 ТО сч1 := Потребителей КОН
   КОН
 КОН;
 КОЛЬЦО
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
     ОТ сч2 := 0 ДО Потребителей-1 ВЫП
       ЕСЛИ недостаток = 0 ТО ВЫХОД ИНАЧЕ
         ЕСЛИ План[сч1, сч2] = -1 ТО
           План[сч1, сч2] := 0; УМЕНЬШИТЬ(недостаток);
         КОН
       КОН
     КОН
   КОН;
   ВЫХОД
 КОН

КОН РасставитьНули;

ЗАДАЧА ЗаполнитьОтУгла; ПЕР

 ОсталосьВНаличии, ОсталосьПотребным: ДОСТУП К РЯД ИЗ ЦЕЛ;
 занято, недостаток: ЦЕЛ;
 сч1, сч2: ЦЕЛ;

УКАЗ

 СОЗДАТЬ(ОсталосьВНаличии, Поставщиков);
 СОЗДАТЬ(ОсталосьПотребным, Потребителей);
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП ОсталосьВНаличии[сч1] := Запасы[сч1] КОН;
 ОТ сч1 := 0 ДО Потребителей-1 ВЫП ОсталосьПотребным[сч1] := Потребности[сч1] КОН;
 занято := 0;
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
   ОТ сч2 := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ ОсталосьВНаличии[сч1] = 0 ТО План[сч1, сч2] := -1 ИНАЧЕ
       ЕСЛИ ОсталосьВНаличии[сч1] > ОсталосьПотребным[сч2] ТО
         ЕСЛИ ОсталосьПотребным[сч2] # 0 ТО План[сч1, сч2] := ОсталосьПотребным[сч2]; УВЕЛИЧИТЬ(занято)
         ИНАЧЕ План[сч1, сч2] := -1 КОН;
         УМЕНЬШИТЬ(ОсталосьВНаличии[сч1], ОсталосьПотребным[сч2]);
         ОсталосьПотребным[сч2] := 0
       ИНАЧЕ
         ЕСЛИ ОсталосьВНаличии[сч1] # 0 ТО План[сч1, сч2] := ОсталосьВНаличии[сч1]; УВЕЛИЧИТЬ(занято)
         ИНАЧЕ План[сч1, сч2] := -1 КОН;
         УМЕНЬШИТЬ(ОсталосьПотребным[сч2], ОсталосьВНаличии[сч1]);
         ОсталосьВНаличии[сч1] := 0
       КОН
     КОН
   КОН
 КОН;
 недостаток := (Поставщиков+Потребителей-1) - занято;
 ЕСЛИ недостаток > 0 ТО РасставитьНули(недостаток) КОН

КОН ЗаполнитьОтУгла;

ЗАДАЧА ОценитьБазисныеКлетки; ПЕР

 сч1, сч2, сч3: ЦЕЛ;
 суммы: ДОСТУП К РЯД ИЗ РЯД 3 ИЗ ЦЕЛ;
 известно: ДОСТУП К РЯД ИЗ РЯД 2 ИЗ КЛЮЧ;

УКАЗ

 СОЗДАТЬ(суммы, Поставщиков+Потребителей-1);
 СОЗДАТЬ(известно, Поставщиков+Потребителей-1);
 известно[0][0] := ВКЛ; известно[0][1] := ОТКЛ;
 ОТ сч1 := 1 ДО (Поставщиков+Потребителей-1)-1 ВЫП известно[сч1][0] := ОТКЛ; известно[сч1][1] := ОТКЛ КОН;
 сч3 := 0;
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
   ОТ сч2 := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ План[сч1, сч2] # -1 ТО
       суммы[сч3][0] := сч1; суммы[сч3][1] := сч2; суммы[сч3][2] := Расходы[сч1, сч2];
       УВЕЛИЧИТЬ(сч3)
     КОН
   КОН
 КОН;
 U[0] := 0;
 ОТ сч1 := 1 ДО (Поставщиков+Потребителей-1)-1 ВЫП
   ЕСЛИ суммы[сч1][0] = 0 ТО известно[сч1][0] := ВКЛ КОН
 КОН;
 сч3 := 0;
 ПОВТОРЯТЬ
   сч1 := 0;
   ПОКА НЕ (известно[сч1][0] # известно[сч1][1]) ВЫП
     УВЕЛИЧИТЬ(сч1)
   КОН;
   ЕСЛИ известно[сч1][0] ТО
     V[суммы[сч1][1]] := суммы[сч1][2] - U[суммы[сч1][0]];
     известно[сч1][1] := ВКЛ;
     ОТ сч2 := 0 ДО (Поставщиков+Потребителей-1)-1 ВЫП
       ЕСЛИ (суммы[сч2][1] = суммы[сч1][1]) И (НЕ известно[сч2][1]) ТО известно[сч2][1] := ВКЛ КОН
     КОН
   ИНАЧЕ
     U[суммы[сч1][0]] := суммы[сч1][2] - V[суммы[сч1][1]];
     известно[сч1][0] := ВКЛ;
     ОТ сч2 := 0 ДО (Поставщиков+Потребителей-1)-1 ВЫП
       ЕСЛИ (суммы[сч2][0] = суммы[сч1][0]) И (НЕ известно[сч2][0]) ТО известно[сч2][0] := ВКЛ КОН
     КОН
   КОН;
   УВЕЛИЧИТЬ(сч3)
 ДО сч3 = Поставщиков+Потребителей-1

КОН ОценитьБазисныеКлетки;

ЗАДАЧА ОценитьСвободныеКлетки(): КЛЮЧ; ПЕР

 сч1, сч2, сч3: ЦЕЛ;
 естьПолож: КЛЮЧ;

УКАЗ

 естьПолож := ОТКЛ;
 сч3 := 0;
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
   ОТ сч2 := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ План[сч1, сч2] = -1 ТО
       оцСв[сч3].значение := U[сч1]+V[сч2]-Расходы[сч1,сч2];
       оцСв[сч3].поставщик := сч1; оцСв[сч3].потребитель := сч2;
       ЕСЛИ оцСв[сч3].значение > 0 ТО естьПолож := ВКЛ КОН;
       УВЕЛИЧИТЬ(сч3)
     КОН
   КОН
 КОН;
 ЕСЛИ естьПолож ТО ВОЗВРАТ ОТКЛ ИНАЧЕ ВОЗВРАТ ВКЛ КОН

КОН ОценитьСвободныеКлетки;

ЗАДАЧА Цикл; ПЕР

 сч1, сч2, сч3: ЦЕЛ;
 максЗн: ЦЕЛ;
 начало, циклНайден: КЛЮЧ;
 ЗАДАЧА НаЛинии(наКакой: ЦЕЛ; столб: КЛЮЧ): ЦЕЛ;
 ПЕР сч, сколько: ЦЕЛ;
 УКАЗ
   сколько := 0;
   ЕСЛИ столб ТО
     ОТ сч := 0 ДО Поставщиков-1 ВЫП
       ЕСЛИ (План[сч, наКакой] # -1) ИЛИ ((сч = начQ_поставщик) И (наКакой = начQ_потребитель)) ТО
         УВЕЛИЧИТЬ(сколько)
       КОН
     КОН
   ИНАЧЕ
     ОТ сч := 0 ДО Потребителей-1 ВЫП
       ЕСЛИ (План[наКакой, сч] # -1) ИЛИ ((наКакой = начQ_поставщик) И (сч = начQ_потребитель)) ТО
         УВЕЛИЧИТЬ(сколько)
       КОН
     КОН
   КОН;
   ВОЗВРАТ сколько
 КОН НаЛинии;
 ЗАДАЧА^ ИскатьВСтолбце(номер, строка: ЦЕЛ): КЛЮЧ;
 ЗАДАЧА ИскатьВСтроке(номер, столбец: ЦЕЛ): КЛЮЧ;
 ПЕР
   сч: ЦЕЛ;
 УКАЗ
   ЕСЛИ (НЕ начало) И (номер = начQ_поставщик) И (столбец = начQ_потребитель) ТО циклНайден := ВКЛ КОН;
   ЕСЛИ начало ТО начало := ОТКЛ КОН;
   ЕСЛИ циклНайден ТО ВОЗВРАТ ВКЛ КОН;
   ОТ сч := 0 ДО Потребителей-1 ВЫП
     ЕСЛИ
       (сч # столбец) И
       ((План[номер, сч] # -1) ИЛИ ((номер = начQ_поставщик) И (сч = начQ_потребитель))) И
       (НаЛинии(сч, ВКЛ) > 1) И
       (Q[номер, сч] = 0)
     ТО
       Q[номер, сч] := -1;
       ЕСЛИ НЕ ИскатьВСтолбце(сч, номер) ТО Q[номер, сч] := 0 ИНАЧЕ ВОЗВРАТ ВКЛ КОН
     КОН
   КОН;
   ВОЗВРАТ ОТКЛ
 КОН ИскатьВСтроке;
 ЗАДАЧА ИскатьВСтолбце(номер, строка: ЦЕЛ): КЛЮЧ;
 ПЕР
   сч: ЦЕЛ;
 УКАЗ
   ЕСЛИ (НЕ начало) И (строка = начQ_поставщик) И (номер = начQ_потребитель) ТО циклНайден := ВКЛ КОН;
   ЕСЛИ начало ТО начало := ОТКЛ КОН;
   ЕСЛИ циклНайден ТО ВОЗВРАТ ВКЛ КОН;
   ОТ сч := 0 ДО Поставщиков-1 ВЫП
     ЕСЛИ
       (сч # строка) И
       ((План[сч, номер] # -1) ИЛИ ((сч = начQ_поставщик) И (номер = начQ_потребитель))) И
       (НаЛинии(сч, ОТКЛ) > 1) И
       (Q[сч, номер] = 0)
     ТО
       Q[сч, номер] := 1;
       ЕСЛИ НЕ ИскатьВСтроке(сч, номер) ТО Q[сч, номер] := 0 ИНАЧЕ ВОЗВРАТ ВКЛ КОН
     КОН
   КОН;
   ВОЗВРАТ ОТКЛ
 КОН ИскатьВСтолбце;

УКАЗ

 максЗн := 0;
 ОТ сч1 := 0 ДО Потребителей*Поставщиков-(Потребителей+Поставщиков-1)-1 ВЫП
   ЕСЛИ оцСв[сч1].значение > максЗн ТО максЗн := оцСв[сч1].значение КОН
 КОН;
 сч3 := 0;
 ПОКА оцСв[сч3].значение # максЗн ВЫП УВЕЛИЧИТЬ(сч3) КОН;
 ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
   ОТ сч2 := 0 ДО Потребителей-1 ВЫП
     Q[сч1, сч2] := 0
   КОН
 КОН;
 Разница := оцСв[сч3].значение;
 начQ_поставщик := оцСв[сч3].поставщик; начQ_потребитель := оцСв[сч3].потребитель;
 начало := ВКЛ; циклНайден := ОТКЛ;
 ЕСЛИ ИскатьВСтроке(начQ_поставщик, начQ_потребитель) ТО КОН

КОН Цикл;

ЗАДАЧА ИзменитьПлан; ПЕР

 сч1, сч2: ЦЕЛ;
 занято, недостаток: ЦЕЛ;

УКАЗ

 ЕСЛИ Поправка = 0 ТО
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
     ОТ сч2 := 0 ДО Потребителей-1 ВЫП
       ЕСЛИ План[сч1, сч2] = 0 ТО План[сч1, сч2] := -1; сч2 := Потребителей; сч1 := Поставщиков КОН
     КОН
   КОН;
   План[начQ_поставщик, начQ_потребитель] := 0
 ИНАЧЕ
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
     ОТ сч2 := 0 ДО Потребителей-1 ВЫП
       ЕСЛИ Q[сч1, сч2] = 1 ТО
         ЕСЛИ План[сч1, сч2] = -1 ТО План[сч1, сч2] := 0 КОН;
         УВЕЛИЧИТЬ(План[сч1, сч2], Поправка);
       АЕСЛИ Q[сч1, сч2] = -1 ТО УМЕНЬШИТЬ(План[сч1, сч2], Поправка)
       КОН
     КОН
   КОН;
   занято := 0;
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
     ОТ сч2 := 0 ДО Потребителей-1 ВЫП
       ЕСЛИ План[сч1, сч2] > 0 ТО УВЕЛИЧИТЬ(занято) КОН
     КОН
   КОН;
   ОТ сч1 := 0 ДО Поставщиков-1 ВЫП
     ОТ сч2 := 0 ДО Потребителей-1 ВЫП
       ЕСЛИ План[сч1, сч2] = 0 ТО План[сч1, сч2] := -1 КОН
     КОН
   КОН;
   недостаток := (Поставщиков+Потребителей-1) - занято;
   ЕСЛИ недостаток > 0 ТО РасставитьНули(недостаток) КОН
 КОН

КОН ИзменитьПлан;

УКАЗ

 ПринятьДанные;
 ЗаполнитьОтУгла;
 Разница := -1;
 КОЛЬЦО
   ОценитьБазисныеКлетки;
   ЕСЛИ ОценитьСвободныеКлетки() ТО ВЫХОД КОН;
   Цикл;
   ПосчитатьПоправку;
   ИзменитьПлан
 КОН;
 ВывестиПлан

КОН Транспорт.</lang>

Input

Number of suppliers: 3.
Number of consumers: 3.
Inventories:
12 40 33
Requirements:
20 30 10
Introduced a fictitious consumer.
The matrix of costs:
3 5 7
2 4 6
9 1 8

Output

--------------------
  -    -    -    12
  20   -    10   10
  -    30   -     3
--------------------

Go

Translation of: Java

<lang go>package main

import (

   "bufio"
   "container/list"
   "fmt"
   "io/ioutil"
   "log"
   "math"
   "os"
   "strconv"

)

type shipment struct {

   quantity, costPerUnit float64
   r, c                  int

}

var shipZero = shipment{}

type transport struct {

   filename       string
   supply, demand []int
   costs          [][]float64
   matrix         [][]shipment

}

func check(err error) {

   if err != nil {
       log.Fatal(err)
   }

}

func minOf(i, j int) int {

   if i < j {
       return i
   }
   return j

}

func newTransport(filename string) *transport {

   file, err := os.Open(filename)
   check(err)
   defer file.Close()
   scanner := bufio.NewScanner(file)
   scanner.Split(bufio.ScanWords)
   scanner.Scan()
   numSources, err := strconv.Atoi(scanner.Text())
   check(err)
   scanner.Scan()
   numDests, err := strconv.Atoi(scanner.Text())
   check(err)
   src := make([]int, numSources)
   for i := 0; i < numSources; i++ {
       scanner.Scan()
       src[i], err = strconv.Atoi(scanner.Text())
       check(err)
   }
   dst := make([]int, numDests)
   for i := 0; i < numDests; i++ {
       scanner.Scan()
       dst[i], err = strconv.Atoi(scanner.Text())
       check(err)
   }
   // fix imbalance
   totalSrc := 0
   for _, v := range src {
       totalSrc += v
   }
   totalDst := 0
   for _, v := range dst {
       totalDst += v
   }
   diff := totalSrc - totalDst
   if diff > 0 {
       dst = append(dst, diff)
   } else if diff < 0 {
       src = append(src, -diff)
   }
   costs := make([][]float64, len(src))
   for i := 0; i < len(src); i++ {
       costs[i] = make([]float64, len(dst))
   }
   matrix := make([][]shipment, len(src))
   for i := 0; i < len(src); i++ {
       matrix[i] = make([]shipment, len(dst))
   }
   for i := 0; i < numSources; i++ {
       for j := 0; j < numDests; j++ {
           scanner.Scan()
           costs[i][j], err = strconv.ParseFloat(scanner.Text(), 64)
           check(err)
       }
   }
   return &transport{filename, src, dst, costs, matrix}

}

func (t *transport) northWestCornerRule() {

   for r, northwest := 0, 0; r < len(t.supply); r++ {
       for c := northwest; c < len(t.demand); c++ {
           quantity := minOf(t.supply[r], t.demand[c])
           if quantity > 0 {
               t.matrix[r][c] = shipment{float64(quantity), t.costs[r][c], r, c}
               t.supply[r] -= quantity
               t.demand[c] -= quantity
               if t.supply[r] == 0 {
                   northwest = c
                   break
               }
           }
       }
   }

}

func (t *transport) steppingStone() {

   maxReduction := 0.0
   var move []shipment = nil
   leaving := shipZero
   t.fixDegenerateCase()
   for r := 0; r < len(t.supply); r++ {
       for c := 0; c < len(t.demand); c++ {
           if t.matrix[r][c] != shipZero {
               continue
           }
           trial := shipment{0, t.costs[r][c], r, c}
           path := t.getClosedPath(trial)
           reduction := 0.0
           lowestQuantity := float64(math.MaxInt32)
           leavingCandidate := shipZero
           plus := true
           for _, s := range path {
               if plus {
                   reduction += s.costPerUnit
               } else {
                   reduction -= s.costPerUnit
                   if s.quantity < lowestQuantity {
                       leavingCandidate = s
                       lowestQuantity = s.quantity
                   }
               }
               plus = !plus
           }
           if reduction < maxReduction {
               move = path
               leaving = leavingCandidate
               maxReduction = reduction
           }
       }
   }
   if move != nil {
       q := leaving.quantity
       plus := true
       for _, s := range move {
           if plus {
               s.quantity += q
           } else {
               s.quantity -= q
           }
           if s.quantity == 0 {
               t.matrix[s.r][s.c] = shipZero
           } else {
               t.matrix[s.r][s.c] = s
           }
           plus = !plus
       }
       t.steppingStone()
   }

}

func (t *transport) matrixToList() *list.List {

   l := list.New()
   for _, m := range t.matrix {
       for _, s := range m {
           if s != shipZero {
               l.PushBack(s)
           }
       }
   }
   return l

}

func (t *transport) getClosedPath(s shipment) []shipment {

   path := t.matrixToList()
   path.PushFront(s)
   // remove (and keep removing) elements that do not have a
   // vertical AND horizontal neighbor
   var next *list.Element
   for {
       removals := 0
       for e := path.Front(); e != nil; e = next {
           next = e.Next()
           nbrs := t.getNeighbors(e.Value.(shipment), path)
           if nbrs[0] == shipZero || nbrs[1] == shipZero {
               path.Remove(e)
               removals++
           }
       }
       if removals == 0 {
           break
       }
   }
   // place the remaining elements in the correct plus-minus order
   stones := make([]shipment, path.Len())
   prev := s
   for i := 0; i < len(stones); i++ {
       stones[i] = prev
       prev = t.getNeighbors(prev, path)[i%2]
   }
   return stones

}

func (t *transport) getNeighbors(s shipment, lst *list.List) [2]shipment {

   var nbrs [2]shipment
   for e := lst.Front(); e != nil; e = e.Next() {
       o := e.Value.(shipment)
       if o != s {
           if o.r == s.r && nbrs[0] == shipZero {
               nbrs[0] = o
           } else if o.c == s.c && nbrs[1] == shipZero {
               nbrs[1] = o
           }
           if nbrs[0] != shipZero && nbrs[1] != shipZero {
               break
           }
       }
   }
   return nbrs

}

func (t *transport) fixDegenerateCase() {

   eps := math.SmallestNonzeroFloat64
   if len(t.supply)+len(t.demand)-1 != t.matrixToList().Len() {
       for r := 0; r < len(t.supply); r++ {
           for c := 0; c < len(t.demand); c++ {
               if t.matrix[r][c] == shipZero {
                   dummy := shipment{eps, t.costs[r][c], r, c}
                   if len(t.getClosedPath(dummy)) == 0 {
                       t.matrix[r][c] = dummy
                       return
                   }
               }
           }
       }
   }

}

func (t *transport) printResult() {

   fmt.Println(t.filename)
   text, err := ioutil.ReadFile(t.filename)
   check(err)
   fmt.Printf("\n%s\n", string(text))
   fmt.Printf("Optimal solution for %s\n\n", t.filename)
   totalCosts := 0.0
   for r := 0; r < len(t.supply); r++ {
       for c := 0; c < len(t.demand); c++ {
           s := t.matrix[r][c]
           if s != shipZero && s.r == r && s.c == c {
               fmt.Printf(" %3d ", int(s.quantity))
               totalCosts += s.quantity * s.costPerUnit
           } else {
               fmt.Printf("  -  ")
           }
       }
       fmt.Println()
   }
   fmt.Printf("\nTotal costs: %g\n\n", totalCosts)

}

func main() {

   filenames := []string{"input1.txt", "input2.txt", "input3.txt"}
   for _, filename := range filenames {
       t := newTransport(filename)
       t.northWestCornerRule()
       t.steppingStone()
       t.printResult()
   }

}</lang>

Output:
input1.txt

2 3
25 35
20 30 10
3 5 7
3 2 5

Optimal solution for input1.txt

  20   -     5 
  -    30    5 

Total costs: 180

input2.txt

3 3
12 40 33
20 30 10
3 5 7
2 4 6
9 1 8

Optimal solution for input2.txt

  -    -    -    12 
  20   -    10   10 
  -    30   -     3 

Total costs: 130

input3.txt

4 4
14 10 15 12
10 15 12 15
10 30 25 15
20 15 20 10
10 30 20 20
30 40 35 45

Optimal solution for input3.txt

  -    -    -    14 
  -     9   -     1 
  10   -     5   -  
  -     5    7   -  
  -     1   -    -  

Total costs: 1000

J

The current task description refers to the algorithm by name, but I feel that those names are ambiguous (inadequately descriptive - we need the algorithm specified here on rosettacode, not problems which we expect are so well understood that no one needs to describe them).

So, I will be working with this interpretation:

(*) Assign shipments for the lowest cost unsatisfied need which can be supplied. When breaking ties, pick the first one which would be encountered when scanning left to right, top to bottom (which is the same order you use when reading english text on a page).

(*) Supply however much of the need which can be supplied by that shipment.

(*) Repeat until done.

(If this algorithm is incorrect for this task, that would just underline the need for a better task description. And, probably, also: the need for a more representative task example.)

In other words:

<lang J>NB. C's y[m] v= x implemented as x m ndxasgn v y ndxasgn=: conjunction define

 ((m{y)v x) m} y

)

trans=: adverb define

 need=. x
 supl=. y
 cost=. m
 dims=. supl ,&# need
 r=. dims$0
 while. 1 e., xfr=. supl *&*/ need do.
   'iS iN'=. ndxs=. dims#:(i. <./), cost % xfr 
   n=. (iS { supl) <. iN { need
   need=. n iN ndxasgn - need
   supl=. n iS ndxasgn - supl
   r=. n (<ndxs)} r
 end.

)</lang>

Task data:

<lang J>need=: 20 30 10 supply=: 25 35 cost=:3 5 7,:3 2 5</lang>

Task example:

<lang J> need cost trans supply 20 0 5

0 30 5</lang>

Java

Works with: Java version 8

<lang java>import java.io.File; import java.util.*; import static java.util.Arrays.stream; import static java.util.stream.Collectors.toCollection;

public class TransportationProblem {

   private static int[] demand;
   private static int[] supply;
   private static double[][] costs;
   private static Shipment[][] matrix;
   private static class Shipment {
       final double costPerUnit;
       final int r, c;
       double quantity;
       public Shipment(double q, double cpu, int r, int c) {
           quantity = q;
           costPerUnit = cpu;
           this.r = r;
           this.c = c;
       }
   }
   static void init(String filename) throws Exception {
       try (Scanner sc = new Scanner(new File(filename))) {
           int numSources = sc.nextInt();
           int numDestinations = sc.nextInt();
           List<Integer> src = new ArrayList<>();
           List<Integer> dst = new ArrayList<>();
           for (int i = 0; i < numSources; i++)
               src.add(sc.nextInt());
           for (int i = 0; i < numDestinations; i++)
               dst.add(sc.nextInt());
           // fix imbalance
           int totalSrc = src.stream().mapToInt(i -> i).sum();
           int totalDst = dst.stream().mapToInt(i -> i).sum();
           if (totalSrc > totalDst)
               dst.add(totalSrc - totalDst);
           else if (totalDst > totalSrc)
               src.add(totalDst - totalSrc);
           supply = src.stream().mapToInt(i -> i).toArray();
           demand = dst.stream().mapToInt(i -> i).toArray();
           costs = new double[supply.length][demand.length];
           matrix = new Shipment[supply.length][demand.length];
           for (int i = 0; i < numSources; i++)
               for (int j = 0; j < numDestinations; j++)
                   costs[i][j] = sc.nextDouble();
       }
   }
   static void northWestCornerRule() {
       for (int r = 0, northwest = 0; r < supply.length; r++)
           for (int c = northwest; c < demand.length; c++) {
               int quantity = Math.min(supply[r], demand[c]);
               if (quantity > 0) {
                   matrix[r][c] = new Shipment(quantity, costs[r][c], r, c);
                   supply[r] -= quantity;
                   demand[c] -= quantity;
                   if (supply[r] == 0) {
                       northwest = c;
                       break;
                   }
               }
           }
   }
   static void steppingStone() {
       double maxReduction = 0;
       Shipment[] move = null;
       Shipment leaving = null;
       fixDegenerateCase();
       for (int r = 0; r < supply.length; r++) {
           for (int c = 0; c < demand.length; c++) {
               if (matrix[r][c] != null)
                   continue;
               Shipment trial = new Shipment(0, costs[r][c], r, c);
               Shipment[] path = getClosedPath(trial);
               double reduction = 0;
               double lowestQuantity = Integer.MAX_VALUE;
               Shipment leavingCandidate = null;
               boolean plus = true;
               for (Shipment s : path) {
                   if (plus) {
                       reduction += s.costPerUnit;
                   } else {
                       reduction -= s.costPerUnit;
                       if (s.quantity < lowestQuantity) {
                           leavingCandidate = s;
                           lowestQuantity = s.quantity;
                       }
                   }
                   plus = !plus;
               }
               if (reduction < maxReduction) {
                   move = path;
                   leaving = leavingCandidate;
                   maxReduction = reduction;
               }
           }
       }
       if (move != null) {
           double q = leaving.quantity;
           boolean plus = true;
           for (Shipment s : move) {
               s.quantity += plus ? q : -q;
               matrix[s.r][s.c] = s.quantity == 0 ? null : s;
               plus = !plus;
           }
           steppingStone();
       }
   }
   static LinkedList<Shipment> matrixToList() {
       return stream(matrix)
               .flatMap(row -> stream(row))
               .filter(s -> s != null)
               .collect(toCollection(LinkedList::new));
   }
   static Shipment[] getClosedPath(Shipment s) {
       LinkedList<Shipment> path = matrixToList();
       path.addFirst(s);
       // remove (and keep removing) elements that do not have a
       // vertical AND horizontal neighbor
       while (path.removeIf(e -> {
           Shipment[] nbrs = getNeighbors(e, path);
           return nbrs[0] == null || nbrs[1] == null;
       }));
       // place the remaining elements in the correct plus-minus order
       Shipment[] stones = path.toArray(new Shipment[path.size()]);
       Shipment prev = s;
       for (int i = 0; i < stones.length; i++) {
           stones[i] = prev;
           prev = getNeighbors(prev, path)[i % 2];
       }
       return stones;
   }
   static Shipment[] getNeighbors(Shipment s, LinkedList<Shipment> lst) {
       Shipment[] nbrs = new Shipment[2];
       for (Shipment o : lst) {
           if (o != s) {
               if (o.r == s.r && nbrs[0] == null)
                   nbrs[0] = o;
               else if (o.c == s.c && nbrs[1] == null)
                   nbrs[1] = o;
               if (nbrs[0] != null && nbrs[1] != null)
                   break;
           }
       }
       return nbrs;
   }
   static void fixDegenerateCase() {
       final double eps = Double.MIN_VALUE;
       if (supply.length + demand.length - 1 != matrixToList().size()) {
           for (int r = 0; r < supply.length; r++)
               for (int c = 0; c < demand.length; c++) {
                   if (matrix[r][c] == null) {
                       Shipment dummy = new Shipment(eps, costs[r][c], r, c);
                       if (getClosedPath(dummy).length == 0) {
                           matrix[r][c] = dummy;
                           return;
                       }
                   }
               }
       }
   }
   static void printResult(String filename) {
       System.out.printf("Optimal solution %s%n%n", filename);
       double totalCosts = 0;
       for (int r = 0; r < supply.length; r++) {
           for (int c = 0; c < demand.length; c++) {
               Shipment s = matrix[r][c];
               if (s != null && s.r == r && s.c == c) {
                   System.out.printf(" %3s ", (int) s.quantity);
                   totalCosts += (s.quantity * s.costPerUnit);
               } else
                   System.out.printf("  -  ");
           }
           System.out.println();
       }
       System.out.printf("%nTotal costs: %s%n%n", totalCosts);
   }
   public static void main(String[] args) throws Exception {
       for (String filename : new String[]{"input1.txt", "input2.txt",
           "input3.txt"}) {
           init(filename);
           northWestCornerRule();
           steppingStone();
           printResult(filename);
       }
   }

}</lang>

input1.txt

2 3
25 35
20 30 10
3 5 7
3 2 5

Optimal solution input1.txt

  20   -     5 
  -    30    5 

Total costs: 180.0
input2.txt

3 3
12 40 33
20 30 10
3 5 7
2 4 6
9 1 8

Optimal solution input2.txt

  -    -    -    12 
  20   -    10   10 
  -    30   -     3 

Total costs: 130.0
input3.txt

4 4
14 10 15 12
10 15 12 15
10 30 25 15
20 15 20 10
10 30 20 20
30 40 35 45

Optimal solution input3.txt

  -    -    -    14 
  -     9   -     1 
  10   -     5   -  
  -     5    7   -  
  -     1   -    -  

Total costs: 1000.0

Julia

Code taken from here using JuMP. <lang julia>using JuMP, Ipopt

  1. cost vector

c = [3, 5, 7, 3, 2, 5]; N = size(c,1);

  1. constraints Ax (<,>,=) b

A = [1 1 1 0 0 0

    0 0 0 1 1 1
    1 0 0 1 0 0
    0 1 0 0 1 0
    0 0 1 0 0 1];

b = [ 25, 35, 20, 30, 10]; s = ['<', '<', '=', '=', '='];

  1. construct model

model = Model(Ipopt.Optimizer) @variable(model, x[i=1:N] >= 0, base_name="traded quantities") cost_fn = @expression(model, c'*x) # cost function @constraint(model, C1, A[1:2,:]*x .<= b[1:2]) # inequality constraints @constraint(model, C2, A[3:5,:]*x .== b[3:5]) # equality constraints @objective(model, Min, cost_fn) # objective function

  1. solve model

status = JuMP.optimize!(model); xstar = value.(x); println("solution vector of quantities = ", xstar) println("minimum total cost = ", JuMP.objective_value(model))

  1. recover Lagrange multipliers for post-optimality

λ = [JuMP.dual(C1[1]),JuMP.dual(C1[2])] μ = [JuMP.dual(C2[1]),JuMP.dual(C2[2]),JuMP.dual(C2[3])]

Output:
solution vector of quantities = [20.000000008747048, 0.0, 4.9999996490783145, 0.0, 30.000000007494098, 5.0000003509216855]
minimum total cost = 179.99999927567436

Racket

Translation of: Java

(I understand the letters in Java!)

Using typed/racket, to keep track of Vectors of Vectors of data.

<lang racket>#lang typed/racket

Translation of: Java

(define-type (V2 A) (Vectorof (Vectorof A))) (define-type VI (Vectorof Integer)) (define-type V2R (V2 Real)) (define-type Q (U 'ε Integer)) (define ε 'ε) (struct Shipment ([qty : Q] [cost/unit : Real] [r : Integer] [c : Integer])) (define-type Shipment/? (Option Shipment)) (define-type V2-Shipment/? (V2 Shipment/?)) (define-type Shipment/?s (Listof Shipment/?)) (define-type Shipments (Listof Shipment))

(: Q+ (Q Q -> Q)) (: Q- (Q Q -> Q)) (: Q<? (Q Q -> Boolean)) (: Q-zero? (Q -> Boolean)) (: Q-unary- (Q -> Q)) (: Q*R (Q Real -> Real))

(define Q+ (match-lambda** [('ε 0) ε] [(0 'ε) ε] [('ε 'ε) ε] [('ε x) x] [(x 'ε) x]

                          [((? integer? x) (? integer? y)) (+ x y)]))

(define Q<? (match-lambda** [('ε 0) #f] [(0 'ε) #t] [('ε 'ε) #f] [('ε x) #t] [(x 'ε) #f]

                           [((? integer? x) (? integer? y)) (< x y)]))

(define Q- (match-lambda** [('ε 0) ε] [(0 'ε) ε] [('ε 'ε) 0] [('ε (? integer? x)) (- x)] [(x 'ε) x]

                          [((? integer? x) (? integer? y)) (- x y)]))

(define Q-unary- (match-lambda ['ε ε] [(? integer? x) (- x)]))

(define Q-zero? (match-lambda ['ε #f] [(? integer? x) (zero? x)]))

(define Q*R (match-lambda** [('ε _) 0] [((? integer? x) y) (* x y)]))

(: vector-ref2 (All (A) ((Vectorof (Vectorof A)) Integer Integer -> A))) (define (vector-ref2 v2 r c) (vector-ref (vector-ref v2 r) c))

(: vector-set!2 (All (A) ((Vectorof (Vectorof A)) Integer Integer A -> Void))) (define (vector-set!2 v2 r c v) (vector-set! (vector-ref v2 r) c v))

(define (northwest-corner-rule! [supply : VI] [demand : VI] [costs : V2R] [M : V2-Shipment/?]) : Void

 (define supply-l (vector-length supply))
 (define demand-l (vector-length demand))
 (let loop ((r 0) (nw 0) (c 0))
   (cond [(= r supply-l) (void)]
         [(= c demand-l) (loop (add1 r) nw 0)]
         [else
          (define quantity (min (vector-ref supply r) (vector-ref demand c)))
          (cond
            [(positive? quantity)
             (define shpmnt (Shipment quantity (vector-ref2 costs r c) r c))
             (vector-set!2 M r c shpmnt)
             (define supply-- (- (vector-ref supply r) quantity))
             (define demand-- (- (vector-ref demand c) quantity))
             (vector-set! supply r supply--)
             (vector-set! demand c demand--)
             (if (zero? supply--) (loop (add1 r) c 0) (loop r nw (add1 c)))]
            [else (loop r nw (add1 c))])])))

(define (stepping-stone! [supply : VI] [demand : VI] [costs : V2R] [M : V2-Shipment/?]) : Void

 (fix-degenerate-case! supply demand costs M)
 (define-values (move leaving max-reduction)
   (for*/fold : (Values Shipments Shipment/? Real)
     ((move : Shipments null) (leaving : Shipment/? #f) (max-reduction : Real 0))
     ((r (vector-length supply))
      (c (vector-length demand))
      (m (in-value (vector-ref2 M r c)))
      #:unless m)
     (define path (let ((trial (Shipment 0 (vector-ref2 costs r c) r c))) (get-closed-path trial M)))
     (define-values (+? reduction leaving-cand lowest-quantity)
       (for/fold : (Values Boolean Real Shipment/? (Option Q))
         ((+? #t) (reduction : Real 0) (leaving-cand : Shipment/? #f) (lowest-q : (Option Q) #f))
         ((s (in-list path)))
         (define s.cpu (Shipment-cost/unit s))
         (if +?
             (values #f (+ reduction s.cpu) leaving-cand lowest-q)
             (let ((reduction-- (- reduction s.cpu))
                   (s.q (Shipment-qty s)))
               (if (or (not lowest-q) (Q<? s.q lowest-q))
                   (values #t reduction-- s s.q)
                   (values #t reduction-- leaving-cand lowest-q))))))
     
     (if (< reduction max-reduction)
         (values path leaving-cand reduction)
         (values move leaving max-reduction))))
 
 (unless (null? move)
   (define l.q (Shipment-qty (cast leaving Shipment)))
   (for/fold ((+? : Boolean #t)) ((s (in-list move)))
     (define s.q+ ((if +? Q+ Q-) (Shipment-qty s) l.q))
     (define s+ (struct-copy Shipment s [qty s.q+]))
     (vector-set!2 M (Shipment-r s+) (Shipment-c s+) (if (Q-zero? s.q+) #f s+))
     (not +?))
   (stepping-stone! supply demand costs M)))

(: matrix->list (All (T) ((V2 T) -> (Listof T)))) (define (matrix->list m)

 (for*/list : (Listof T) ((r (in-vector m)) (c (in-vector r)) #:when c)
   c))

(define (fix-degenerate-case! [supply : VI] [demand : VI] [costs : V2R] [M : V2-Shipment/?]) : Void

 (define m-list (matrix->list M))
 (unless (= (+ (vector-length supply) (vector-length demand) -1) (length m-list))
   (let/ec ret : Void
     (for* ((r (vector-length supply)) (c (vector-length demand)) #:unless (vector-ref2 M r c))
       (define dummy (Shipment ε (vector-ref2 costs r c) r c))
       (when (null? (get-closed-path dummy M))
         (vector-set!2 M r c dummy)
         (ret (void)))))))

(: get-closed-path (Shipment V2-Shipment/? -> Shipments)) (define (get-closed-path s matrix)

 ; remove (and keep removing) elements that do not have a vertical AND horizontal neighbour
 (define path
   (let loop : Shipment/?s
     ((path (cons s (matrix->list matrix))))
     (define (has-neighbours [e : Shipment/?]) : Boolean
       (match-define (list n0 n1) (get-neighbours e path))
       (and n0 n1 #t))
     (define-values (with-nbrs w/o-nbrs)
       ((inst partition Shipment/? Shipment/?) has-neighbours path))
     (if (null? w/o-nbrs) with-nbrs (loop with-nbrs))))
 
 ;; place the remaining elements in the correct plus-minus order
 (define p-len (length path))
 (define-values (senots prev)
   (for/fold : (Values Shipments Shipment/?)
     ((senots : Shipments null) (prev : Shipment/? s))
     ((i p-len))
     (values (if prev (cons prev senots) senots)
             (list-ref (get-neighbours prev path) (modulo i 2)))))
 (reverse senots))

(define (get-neighbours [s : Shipment/?] [lst : Shipment/?s]) : (List Shipment/? Shipment/?)

 (define-values (n0 n1)
   (for/fold : (Values Shipment/? Shipment/?)
     ((n0 : Shipment/? #f) (n1 : Shipment/? #f))
     ((o (in-list lst)) #:when (and o s) #:unless (equal? o s))              
     (values (or n0 (and (= (Shipment-r s) (Shipment-r o)) o))
             (or n1 (and (= (Shipment-c s) (Shipment-c o)) o)))))
 (list n0 n1))

(define (print-result [S : VI] [D : VI] [M : V2-Shipment/?] [fmt : String] . [args : Any *]) : Real

 (apply printf (string-append fmt "~%") args)
 (define total-costs
   (for*/sum : Real
     ((r (vector-length S)) (c (vector-length D)))
     (when (zero? c) (unless (zero? r) (newline)))
     (define s (vector-ref2 M r c))
     (cond
       [(and s (= (Shipment-r s) r) (= (Shipment-c s) c))
        (define q (Shipment-qty s))
        (printf "\t~a" q)
        (Q*R q (Shipment-cost/unit s))]
       [else (printf "\t-") 0])))
 (printf "~%Total costs: ~a~%~%" total-costs)
 total-costs)
inits from current-input-port --- make sure you set that before coming in

(define (init) : (Values VI VI V2R V2-Shipment/?)

 (define n-sources (cast (read) Integer))
 (define n-destinations (cast (read) Integer))
 (define srcs. (for/list : (Listof Integer) ((_ n-sources)) (cast (read) Integer)))
 (define dsts. (for/list : (Listof Integer) ((_ n-destinations)) (cast (read) Integer)))
 
 (define sum-src--sum-dest (- (apply + srcs.) (apply + dsts.)))
 
 (define-values (supply demand)
   (cond [(positive? sum-src--sum-dest) (values srcs. (append dsts. (list sum-src--sum-dest)))]
         [(negative? sum-src--sum-dest) (values (append srcs. (list (- sum-src--sum-dest))) dsts.)]
         [else (values srcs. dsts.)]))
 
 (define s-l (length supply))
 (define d-l (length demand))
 (define costs (for/vector : V2R ((_ s-l)) ((inst make-vector Real) d-l 0)))
 (define matrix (for/vector : V2-Shipment/? ((_ s-l)) ((inst make-vector Shipment/?) d-l #f)))
 (for* ((i n-sources) (j n-destinations)) (vector-set!2 costs i j (cast (read) Real)))
 (values (list->vector supply) (list->vector demand) costs matrix))

(: transportation-problem (Input-Port -> Real)) (define (transportation-problem p)

 (parameterize ([current-input-port p])
   (define name (read))
   (define-values (supply demand costs matrix) (init))
   (northwest-corner-rule! supply demand costs matrix)
   (stepping-stone! supply demand costs matrix)
   (print-result supply demand matrix "Optimal solutions for: ~s" name)))

(module+ test

 (require typed/rackunit)
 (define (check-tp [in-str : String] [expected-cost : Real])
   (define cost ((inst call-with-input-string Real) in-str transportation-problem))
   (check-equal? cost expected-cost))
 (check-tp #<<$

input1

2 3

25 35 20 30 10 3 5 7 3 2 5 $

           180)
 
 (check-tp #<<$

input2 3 3 12 40 33 20 30 10 3 5 7 2 4 6 9 1 8 $

           130)
 (check-tp #<<$

input3 4 4 14 10 15 12 10 15 12 15 10 30 25 15 20 15 20 10 10 30 20 20 30 40 35 45 $

           1000))</lang>
Output:

Output of: raco test Transportation-problem.rkt:

raco test: (submod "transportation-problem.rkt" test)
Optimal solutions for: input1
        20      -       5
        -       30      5
Total costs: 180

Optimal solutions for: input2
        -       -       -       12
        20      -       10      10
        -       30      -       3
Total costs: 130

Optimal solutions for: input3
        -       -       -       14
        -       9       -       1
        10      -       5       -
        -       5       7       -
        -       1       -       -
Total costs: 1000

3 tests passed

Raku

(formerly Perl 6)

Works with: Rakudo version 2019.03.1

Using Vogel's approximation method: <lang perl6>my %costs = :S1{:3C1, :5C2, :7C3}, :S2{:3C1, :2C2, :5C3}; my %demand = :20C1, :30C2, :10C3; my %supply = :25S1, :35S2;

my @cols = %demand.keys.sort;

my %res; my %g = (|%supply.keys.map: -> $x { $x => [%costs{$x}.sort(*.value)».key]}),

  (|%demand.keys.map: -> $x { $x => [%costs.keys.sort({%costs{$_}{$x}})]});

while (+%g) {

   my @d = %demand.keys.map: -> $x
     {[$x, my $z = %costs{%g{$x}[0]}{$x},%g{$x}[1] ?? %costs{%g{$x}[1]}{$x} - $z !! $z]}
   my @s = %supply.keys.map: -> $x
     {[$x, my $z = %costs{$x}{%g{$x}[0]},%g{$x}[1] ?? %costs{$x}{%g{$x}[1]} - $z !! $z]}
   @d = |@d.grep({ (.[2] == max @d».[2]) }).&min: :by(*.[1]);
   @s = |@s.grep({ (.[2] == max @s».[2]) }).&min: :by(*.[1]);
   my ($t, $f) = @d[2] == @s[2] ?? (@s[1],@d[1]) !! (@d[2],@s[2]);
   my ($d, $s) = $t > $f ?? (@d[0],%g{@d[0]}[0]) !! (%g{@s[0]}[0], @s[0]);
   my $v = %supply{$s} min %demand{$d};
   %res{$s}{$d} += $v;
   %demand{$d} -= $v;
   if (%demand{$d} == 0) {
       %supply.grep( *.value != 0 )».key.map: -> $v
         { %g{$v}.splice((%g{$v}.first: * eq $d, :k),1) };
       %g{$d}:delete;
       %demand{$d}:delete;
   }
   %supply{$s} -= $v;
   if (%supply{$s} == 0) {
       %demand.grep( *.value != 0 )».key.map: -> $v
         { %g{$v}.splice((%g{$v}.first: * eq $s, :k),1) };
       %g{$s}:delete;
       %supply{$s}:delete;
   }

}

say join "\t", flat , @cols; my $total; for %costs.keys.sort -> $g {

   print "$g\t";
   for @cols -> $col {
       print %res{$g}{$col} // '-', "\t";
       $total += (%res{$g}{$col} // 0) * %costs{$g}{$col};
   }
   print "\n";

} say "\nTotal cost: $total";</lang>

Output:
	C1	C2	C3
S1	20	-	5	
S2	-	30	5	

SAS

Use network solver in SAS/OR:

<lang sas>/* create SAS data sets */ data cost_data;

  input from $ to $ cost; 
  datalines;

s1 c1 3 s1 c2 5 s1 c3 7 s2 c1 3 s2 c2 2 s2 c3 5

data supply_data;

  input node $ supply;
  datalines;

s1 25 s2 35 c1 -20 c2 -30 c3 -10

/* call OPTMODEL procedure in SAS/OR */ proc optmodel;

  /* declare sets and parameters, and read input data */
  set <str,str> LINKS;
  num cost {LINKS};
  read data cost_data into LINKS=[from to] cost;
  set NODES = union {<i,j> in LINKS} {i,j};
  num supply {NODES} init 0;
  read data supply_data into [node] supply;
  num flow {LINKS};
  /* call network solver */
  solve with network /
     mincostflow links=(weight=cost) nodes=(weight=supply) direction=directed out=(flow=flow);
  /* print optimal solution */
  print _OROPTMODEL_NUM_['OBJECTIVE'];
  print flow;

quit;</lang>

Output:

180 

flow 
  c1 c2 c3 
s1 20 0 5 
s2 0 30 5 

Visual Basic .NET

Translation of: C#

<lang vbnet>Module Module1

   Class Shipment
       Public Sub New(q As Double, cpu As Double, rv As Integer, cv As Integer)
           Quantity = q
           CostPerUnit = cpu
           R = rv
           C = cv
       End Sub
       Public ReadOnly Property CostPerUnit() As Double
       Public Property Quantity() As Double
       Public ReadOnly Property R As Integer
       Public ReadOnly Property C As Integer
       Public Shared Operator =(s1 As Shipment, s2 As Shipment) As Boolean
           Return s1.CostPerUnit = s2.CostPerUnit _
               AndAlso s1.Quantity = s2.Quantity _
               AndAlso s1.R = s2.R _
               AndAlso s1.C = s2.C
       End Operator
       Public Shared Operator <>(s1 As Shipment, s2 As Shipment) As Boolean
           Return s1.CostPerUnit <> s2.CostPerUnit _
               OrElse s1.Quantity <> s2.Quantity _
               OrElse s1.R <> s2.R _
               OrElse s1.C <> s2.C
       End Operator
   End Class
   Class Program
       Private demand() As Integer
       Private supply() As Integer
       Private costs(,) As Double
       Private matrix(,) As Shipment
       Sub Init(filename As String)
           Dim file = My.Computer.FileSystem.OpenTextFileReader(filename)
           Dim line = file.ReadLine
           Dim numArr = line.Split
           Dim numSources = Integer.Parse(numArr(0))
           Dim numDestinations = Integer.Parse(numArr(1))
           Dim src As New List(Of Integer)
           Dim dst As New List(Of Integer)
           line = file.ReadLine
           numArr = line.Split
           For i = 1 To numSources
               src.Add(Integer.Parse(numArr(i - 1)))
           Next
           line = file.ReadLine
           numArr = line.Split
           For i = 1 To numDestinations
               dst.Add(Integer.Parse(numArr(i - 1)))
           Next
           REM fix imbalance
           Dim totalSrc = src.Sum
           Dim totalDst = dst.Sum
           If totalSrc > totalDst Then
               dst.Add(totalSrc - totalDst)
           ElseIf totalDst > totalSrc Then
               src.Add(totalDst - totalSrc)
           End If
           supply = src.ToArray
           demand = dst.ToArray
           ReDim costs(supply.Length - 1, demand.Length - 1)
           ReDim matrix(supply.Length - 1, demand.Length - 1)
           For i = 1 To numSources
               line = file.ReadLine
               numArr = line.Split
               For j = 1 To numDestinations
                   costs(i - 1, j - 1) = Integer.Parse(numArr(j - 1))
               Next
           Next
       End Sub
       Sub NorthWestCornerRule()
           Dim northwest = 1
           For r = 1 To supply.Length
               For c = northwest To demand.Length
                   Dim quantity = Math.Min(supply(r - 1), demand(c - 1))
                   If quantity > 0 Then
                       matrix(r - 1, c - 1) = New Shipment(quantity, costs(r - 1, c - 1), r - 1, c - 1)
                       supply(r - 1) -= quantity
                       demand(c - 1) -= quantity
                       If supply(r - 1) = 0 Then
                           northwest = c
                           Exit For
                       End If
                   End If
               Next
           Next
       End Sub
       Sub SteppingStone()
           Dim maxReduction = 0.0
           Dim move() As Shipment = Nothing
           Dim leaving As Shipment = Nothing
           FixDegenerateCase()
           For r = 1 To supply.Length
               For c = 1 To demand.Length
                   If Not IsNothing(matrix(r - 1, c - 1)) Then
                       Continue For
                   End If
                   Dim trial As New Shipment(0, costs(r - 1, c - 1), r - 1, c - 1)
                   Dim path = GetClosedPath(trial)
                   Dim reduction = 0.0
                   Dim lowestQuanity = Integer.MaxValue
                   Dim leavingCandidate As Shipment = Nothing
                   Dim plus = True
                   For Each s In path
                       If plus Then
                           reduction += s.CostPerUnit
                       Else
                           reduction -= s.CostPerUnit
                           If s.Quantity < lowestQuanity Then
                               leavingCandidate = s
                               lowestQuanity = s.Quantity
                           End If
                       End If
                       plus = Not plus
                   Next
                   If reduction < maxReduction Then
                       move = path
                       leaving = leavingCandidate
                       maxReduction = reduction
                   End If
               Next
           Next
           If Not IsNothing(move) Then
               Dim q = leaving.Quantity
               Dim plus = True
               For Each s In move
                   s.Quantity += If(plus, q, -q)
                   matrix(s.R, s.C) = If(s.Quantity = 0, Nothing, s)
                   plus = Not plus
               Next
               SteppingStone()
           End If
       End Sub
       Sub FixDegenerateCase()
           Const eps = Double.Epsilon
           If supply.Length + demand.Length - 1 <> MatrixToList().Count Then
               For r = 1 To supply.Length
                   For c = 1 To demand.Length
                       If IsNothing(matrix(r - 1, c - 1)) Then
                           Dim dummy As New Shipment(eps, costs(r - 1, c - 1), r - 1, c - 1)
                           If GetClosedPath(dummy).Length = 0 Then
                               matrix(r - 1, c - 1) = dummy
                               Return
                           End If
                       End If
                   Next
               Next
           End If
       End Sub
       Function MatrixToList() As List(Of Shipment)
           Dim newList As New List(Of Shipment)
           For Each item In matrix
               If Not IsNothing(item) Then
                   newList.Add(item)
               End If
           Next
           Return newList
       End Function
       Function GetClosedPath(s As Shipment) As Shipment()
           Dim path = MatrixToList()
           path.Add(s)
           REM remove (and keep removing) elements that do not have a veritcal AND horizontal neighbor
           Dim before As Integer
           Do
               before = path.Count
               path.RemoveAll(Function(ship)
                                  Dim nbrs = GetNeighbors(ship, path)
                                  Return IsNothing(nbrs(0)) OrElse IsNothing(nbrs(1))
                              End Function)
           Loop While before <> path.Count
           REM place the remaining elements in the correct plus-minus order
           Dim stones = path.ToArray
           Dim prev = s
           For i = 1 To stones.Length
               stones(i - 1) = prev
               prev = GetNeighbors(prev, path)((i - 1) Mod 2)
           Next
           Return stones
       End Function
       Function GetNeighbors(s As Shipment, lst As List(Of Shipment)) As Shipment()
           Dim nbrs() As Shipment = {Nothing, Nothing}
           For Each o In lst
               If o <> s Then
                   If o.R = s.R AndAlso IsNothing(nbrs(0)) Then
                       nbrs(0) = o
                   ElseIf o.C = s.C AndAlso IsNothing(nbrs(1)) Then
                       nbrs(1) = o
                   End If
                   If Not IsNothing(nbrs(0)) AndAlso Not IsNothing(nbrs(1)) Then
                       Exit For
                   End If
               End If
           Next
           Return nbrs
       End Function
       Sub PrintResult(filename As String)
           Console.WriteLine("Optimal solution {0}" + vbNewLine, filename)
           Dim totalCosts = 0.0
           For r = 1 To supply.Length
               For c = 1 To demand.Length
                   Dim s = matrix(r - 1, c - 1)
                   If Not IsNothing(s) AndAlso s.R = r - 1 AndAlso s.C = c - 1 Then
                       Console.Write(" {0,3} ", s.Quantity)
                       totalCosts += (s.Quantity * s.CostPerUnit)
                   Else
                       Console.Write("  -  ")
                   End If
               Next
               Console.WriteLine()
           Next
           Console.WriteLine(vbNewLine + "Total costs: {0}" + vbNewLine, totalCosts)
       End Sub
   End Class
   Sub Main()
       For Each filename In {"input1.txt", "input2.txt", "input3.txt"}
           Dim p As New Program
           p.Init(filename)
           p.NorthWestCornerRule()
           p.SteppingStone()
           p.PrintResult(filename)
       Next
   End Sub

End Module</lang>

Output:
Optimal solution input1.txt

  20   -     5
  -    30    5

Total costs: 180

Optimal solution input2.txt

  -    -    -    12
  20   -    10   10
  -    30   -     3

Total costs: 130

Optimal solution input3.txt

  -    -    -    14
  -     9   -     1
  10   -     5   -
  -     5    7   -
  -     1   -    -

Total costs: 1000