



let post = [90,400,110];
let potr = [140,300,160];
let n =3;
let m =3;
let vir = m+n-1;
let tarif = [
  [ 2, 5, 2],
  [ 4, 1, 5],
  [ 3, 6, 8]
];
let sum1 =0;
let sum2 =0;
for(let i = 0; i < post.length; i++){
    sum1 = sum1+ post[i];   
}
for(let i = 0; i < potr.length; i++){
    sum2 = sum2+ post[i];   
}
if(sum1 == sum2){
    console.log("Задача закрытая");
}
else if(sum1 != sum2){
    console.log("Задача открытая");
}


let tarif1 = tarif.slice();
let tar = tarif.slice();
let sum = 0;
let count = 0;

while (post.reduce((acc, val) => acc + val, 0) > 0 && potr.reduce((acc, val) => acc + val, 0) > 0) {
    let buffI = 0;
    let buffJ = 0;
    let min = 999999;

    for (let i = 0; i < tarif.length; i++) {
        for (let j = 0; j < tarif[i].length; j++) {
            if (tarif[i][j] < min) {
                if (potr[j] > 0 && post[i] > 0) {
                    buffJ = j;
                    buffI = i;
                    min = tarif[i][j];
                }
            }
        }
    }

    if (post[buffI] >= potr[buffJ]) {
        tar[buffI][buffJ] = tarif[buffI][buffJ];
        tarif1[buffI][buffJ] = potr[buffJ];
        post[buffI] -= potr[buffJ];
        potr[buffJ] = 0;
        sum += tarif1[buffI][buffJ] * min;
        count++;
    } else if (post[buffI] < potr[buffJ] && post[buffI] !== 0) {
        tar[buffI][buffJ] = tarif[buffI][buffJ];
        potr[buffJ] -= post[buffI];
        tarif1[buffI][buffJ] = post[buffI];
        post[buffI] = 0;
        sum += tarif1[buffI][buffJ] * min;
        count++;
    }
}
if(count == vir){
    console.log("опорный план вырожденный");
}
else{
    console.log("опорный план невырожденный");
}


console.log("Целевая функция = " + sum);
console.log("Матрица совершенных поставок:");
for (let i = 0; i < tarif1.length; i++) {
    console.log(tarif1[i].join(" \t "));
}

console.log();
console.log("Матрица используемых тарифов:");
for (let i = 0; i < tar.length; i++) {
    console.log(tar[i].join(" \t "));
}
