using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MinEl { internal class MinimalEl { double[][] arrayTarif; int[] suppliers, customers; int[][] arraydelivery; bool[][] checkCell; double targetFunction; public MinimalEl(double[][] arrayTarif, int[][] supliersAndCustomers) { this.arrayTarif = arrayTarif; suppliers = supliersAndCustomers[0]; customers = supliersAndCustomers[1]; arraydelivery = new int[arrayTarif.Count()][]; checkCell = new bool[arrayTarif.Count()][]; for (int i = 0; i < arrayTarif.Count(); i++) { arraydelivery[i] = new int[arrayTarif[i].Count()]; checkCell[i] = new bool[arrayTarif[i].Count()]; } for (int i = 0; i < arraydelivery.Count(); i++) { for (int j = 0; j < arraydelivery[i].Count(); j++) checkCell[i][j] = false; } } void InsertCelslAfterDelivery(int delivery, int indexRow, int indexCol) { arraydelivery[indexRow][indexCol] = delivery; checkCell[indexRow][indexCol] = true; if (suppliers[indexRow] == 0) { for (int i = 0; i < arrayTarif[0].Count(); i++) { if (arraydelivery[indexRow][i] != delivery & arraydelivery[indexRow][i]==0) { arraydelivery[indexRow][i] = 0; checkCell[indexRow][i] = true; } } } if (customers[indexCol] == 0) { for (int i = 0; i < arrayTarif.Count(); i++) { if (arraydelivery[i][indexCol] != delivery & arraydelivery[i][indexCol] == 0) { arraydelivery[i][indexCol] = 0; checkCell[i][indexCol] = true; } } } } public void TargetFunction() { for (int i = 0; i < arraydelivery.Count(); i++) { for (int j = 0; j < arraydelivery[i].Count(); j++) if (arraydelivery[i][j]!=0) targetFunction += arraydelivery[i][j]*arrayTarif[i][j]; } } public void RealizationAlg() { while (true) { if (checkCell.All(row => row.All(el => el == true))) break; double minTarif = int.MaxValue; int indexRow = int.MaxValue; int indexCol = int.MaxValue; for (int i = 0; i < arrayTarif.Count(); i++) { for (int j = 0; j < arrayTarif[i].Count(); j++) { if (minTarif > arrayTarif[i][j] & !checkCell[i][j]) { minTarif = arrayTarif[i][j]; indexRow = i; indexCol = j; } } } int delivery = Math.Min(suppliers[indexRow], customers[indexCol]); suppliers[indexRow]-=delivery; customers[indexCol]-=delivery; InsertCelslAfterDelivery(delivery, indexRow, indexCol); } TargetFunction(); } public override string ToString() { string optPlan = string.Empty; foreach (int[] line in arraydelivery) { foreach (int item in line) optPlan += $"{item} "; optPlan +="\n"; } return $"{optPlan}\n{targetFunction}"; } } }