int NorthWest int storage1 int shop1 int shopSize int storageSize int

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
int NorthWest(int *storage1, int *shop1, int shopSize, int storageSize, int **emptyMas, int **mas){
int *storage = (int*)malloc(storageSize * sizeof(int));
int *shop = (int*)malloc(shopSize * sizeof(int));
for ( int i = 0; i < storageSize; i++)
storage[i] = storage1[i];
for ( int i = 0; i < shopSize; i++)
shop[i] = shop1[i];
int storageIndex = 0, shopIndex = 0, sum = 0;
while ((storageIndex < storageSize) && (shopIndex < shopSize)){
if (storage[storageIndex] > shop[shopIndex]){
emptyMas[storageIndex][shopIndex] = shop[shopIndex];
storage[storageIndex] -= shop[shopIndex];
sum += mas[storageIndex][shopIndex] * emptyMas[storageIndex][shopIndex];
shopIndex++;
}
else {
emptyMas[storageIndex][shopIndex] = storage[storageIndex];
shop[shopIndex] -= storage[storageIndex];
sum += mas[storageIndex][shopIndex] * emptyMas[storageIndex][shopIndex];
storageIndex++;
}
}
free(shop);
free(storage);
return sum;
}
int MinPrice(int *storage1, int *shop1, int shopSize, int storageSize, int **emptyMas, int **mas){
int *storage = (int*)malloc(storageSize * sizeof(int));
int *shop = (int*)malloc(shopSize * sizeof(int));
for ( int i = 0; i < storageSize; i++)
storage[i] = storage1[i];
for ( int i = 0; i < shopSize; i++)
shop[i] = shop1[i];
int sum = 0, nmiI = 0, nmiJ = 0, i = 0, j = 0;
bool flag = true;
bool *I = (bool*)calloc(storageSize, sizeof(bool));
bool *J = (bool*)calloc(shopSize, sizeof(bool));
while (flag){
flag = false;
for ( i = 0; i < storageSize; i++){
if(!I[i]){
nmiI = i;
break;
}
}
for ( i = 0; i < shopSize; i++){
if (!J[i]){
nmiJ = i;
break;
}
}
for ( i = 0; i < storageSize; i++){
for ( j = 0; j < shopSize; j++){
if (mas[nmiI][nmiJ] >= mas[i][j] && !I[i] && !J[j]){
nmiI = i;
nmiJ = j;
flag = true;
}
}
}
if (flag){
if(storage[nmiI] > shop[nmiJ]){
emptyMas[nmiI][nmiJ] = shop[nmiJ];
storage[nmiI] -= shop[nmiJ];
sum += mas[nmiI][nmiJ] * emptyMas[nmiI][nmiJ];
J[nmiJ] = true;
}
else{
emptyMas[nmiI][nmiJ] = storage[nmiI];
shop[nmiJ] -= storage[nmiI];
sum += mas[nmiI][nmiJ] * emptyMas[nmiI][nmiJ];
I[nmiI] = true;
}
}
}
free(storage);
free(shop);
free(I);
free(J);
return sum;
}