-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHungarianAlgo.cpp
66 lines (61 loc) · 1.16 KB
/
HungarianAlgo.cpp
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
#include <iostream>
#include <limits.h>
#define n 4
int getMinCost(int matrix[n][n])
{
int cost=0,i=0,j=0;
int min =INT_MAX;
int rowIndex=0;colIndex=0;
//step1
//substract the smallest of every row from each element of the row
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(inputArray[j][i]<min)
{
min=inputArray[j][i];
// colIndex=i;//ith index is minimum element for s
}
}
for(i=0;i<n;i++)
{
matrix[j][i]-=min;
}
}
//step2
min=INT_MAX;
// for(int i=0;i<n;i++)
// {
// if(inputArray[i][colIndex]<min){
// min=inputArray[i][colIndex];
// rowIndex=i;
// }
// }
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(inputArray[i][j]<min)
{
min=inputArray[i][j];
// colIndex=i;//ith index is minimum element for s
}
}
for(i=0;i<n;i++)
{
matrix[i][j]-=min;
}
}
return cost;
}
int main()
{
int i=0,j=0;
cost_matrix[n][n]={ {9,2,7,8},
{6,4,3,7},
{5,8,1,8},
{7,6,9,4}
};
cout<<("The minimum cost is: ")<<getMinCost(cost_matrix)<<"\n";
}