-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHungaro.cpp
263 lines (223 loc) · 7.43 KB
/
Hungaro.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <bits/stdc++.h>
using namespace std;
class Hungaro{
private:
int n, maxMatching;
vector<vector<int> > matrixAdj, origMatrixAdj;
vector<int> tag[2], father[2];
void printStringSquare(string s){
// tam of 5 chars
int leftover = 5 - int(s.size());
int firstPrint = leftover/2;
cout << string(firstPrint, ' ') << s << string(leftover-firstPrint, ' ');
}
string intToRoman(int a){
// less than 3999
string ans;
string M[] = {"","M","MM","MMM"};
string C[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
string X[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
string I[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
ans = M[a/1000]+C[(a%1000)/100]+X[(a%100)/10]+I[(a%10)];
return ans;
}
void print(){
// n less than 26
cout << '\n';
cout << " Actual Matching = " << maxMatching << '\n';
for(int i = -1; i < n; i++){
if(i == -1){
printStringSquare("");
for(int j = 1; j <= n; j++)
printStringSquare(intToRoman(j));
cout << '\n';
continue;
}
printStringSquare(string(1, 'A' + i));
for(int j = 0; j < n; j++){
string s = to_string(matrixAdj[i][j]);
if(tag[0][i] == j){
s = "[" + s + "]";
assert(tag[1][j] == i);
}
printStringSquare(s);
}
printStringSquare(father[0][i] == -1 ? "" : (father[0][i] == -2 ? "s" : intToRoman(father[0][i]+1)));
cout << '\n';
}
for(int j = -1; j < n; j++){
if(j == -1) printStringSquare("");
else printStringSquare(father[1][j] == -1 ? "" : string(1, 'A' + father[1][j]));
}
cout << '\n';
}
void readInput(){
// n
// 13 0 1 ... 1944
// ...
// 485 2 1 ... 1
// Every row and column has one or more 1's
// n = m
// There is a perfect matching
maxMatching = 0;
cin >> n;
matrixAdj.resize(n, vector<int> (n, 0));
tag[0].resize(n, -1), tag[1].resize(n, -1);
father[0].resize(n, -1), father[1].resize(n, -1);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> matrixAdj[i][j];
origMatrixAdj = matrixAdj;
}
void firstMatching(){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(matrixAdj[i][j] == 0 && tag[1][j] == -1){
maxMatching++;
tag[1][j] = i;
tag[0][i] = j;
break;
}
}
}
}
void extendMatching(int x, bool isColumn){
if(isColumn){
maxMatching++;
tag[1][x] = father[1][x];
extendMatching(tag[1][x], 0);
tag[0][tag[1][x]] = x;
}else if(tag[0][x] == -1) return;
else{
maxMatching--;
extendMatching(tag[0][x], 1);
}
}
void matrixChange(queue<pair<int, bool> > &q){
cout << "\nNO SE PUEDE EXTENDER EL MATCHING PARA LOGRAR UN MATCHING PERFECTO Y SE LLEGA A:\n";
print();
set<int> s,gamma;
// the queue is only tagged rows
while(!q.empty()) q.pop();
for(int i = 0; i < n; i++)
if(father[0][i] != -1){
q.push({i, 0});
s.insert(i);
gamma.insert(father[0][i]);
}
// sum and substract into the adjacency matrix
int mini = 1e9;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(s.count(i) && !gamma.count(j))
mini = min(mini, matrixAdj[i][j]);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
if(s.count(i)&&!gamma.count(j)) matrixAdj[i][j] -= mini;
else if(!s.count(i)&&gamma.count(j)) matrixAdj[i][j] += mini;
}
cout << "\nTENEMOS QUE S = { ";
for(int x:s) cout << string(1, 'A' + x) << ' ';
cout << "} Y Gamma(S) = { ";
for(int x:gamma) if(x != -2) cout << intToRoman(x+1) << ' ';
cout << "}\n";
// print new matrix
cout << "\nSE HACE UN CAMBIO DE MATRIZ OBTENIENDO LA NUEVA:\n";
print();
}
void stepQueue(queue<pair<int, bool> > &q){
while(!q.empty()){
// print(); // --> ALL STEPS
pair<int, bool> y = q.front(); int x = y.first;
q.pop();
if(y.second == 0){ // isRow
for(int j = 0; j < n; j++){
if(matrixAdj[x][j] == 0 && j != tag[0][x] && father[1][j] == -1){
father[1][j] = x;
q.push({j, 1});
}
}
}else{ // isColumn
if(tag[1][x] == -1){ // extend matching
int antMaxMatching = maxMatching;
extendMatching(x, 1);
assert(maxMatching == antMaxMatching+1);
break;
}else if(father[0][tag[1][x]] == -1){
father[0][tag[1][x]] = x;
q.push({tag[1][x], 0});
}
}
}
}
void stepMaxMatching(){
queue<pair<int, bool> > q; // (number, isColumn)
// search the first row
for(int i = 0; i < n; i++){
if(tag[0][i] == -1){
father[0][i] = -2;
q.push({i, 0});
}
}
int antMaxMatching;
while(true){
antMaxMatching = maxMatching;
stepQueue(q);
if(antMaxMatching == maxMatching){ // matrix change
matrixChange(q);
}else break;
}
print();
father[0].assign(n, -1), father[1].assign(n, -1);
}
int minSum(){
matrixAdj = origMatrixAdj;
int sum = 0;
for(int i = 0; i < n; i++){
assert(tag[0][i] != -1);
sum += matrixAdj[i][tag[0][i]];
}
cout << string(60, '_') << '\n';
cout << " LA RESPUESTA ES LA SIGUIENTE:\n";
print();
cout << " CON UNA SUMA DE " << sum << "\n\n";
return sum;
}
void firstSumAndSubstract(){
for(int i = 0; i < n; i++){
int mini = 1e9;
for(int j = 0; j < n; j++)
mini = min(mini, matrixAdj[i][j]);
for(int j = 0; j < n; j++)
matrixAdj[i][j] -= mini;
}
for(int i = 0; i < n; i++){
int mini = 1e9;
for(int j = 0; j < n; j++)
mini = min(mini, matrixAdj[j][i]);
for(int j = 0; j < n; j++)
matrixAdj[j][i] -= mini;
}
}
public:
Hungaro(){ readInput(); }
int calcMinSumMaxMatching(){
firstSumAndSubstract();
print();
cout << string(40, '=') << '\n';
firstMatching();
print();
do{
cout << '\n' << string(40, '=') << '\n';
stepMaxMatching();
}while(n != maxMatching);
assert(maxMatching == n);
return minSum();
}
};
int main(){
Hungaro *matching = new Hungaro();
matching->calcMinSumMaxMatching();
delete matching;
return 0;
}