-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHungarian.cpp
More file actions
81 lines (62 loc) · 1.77 KB
/
Hungarian.cpp
File metadata and controls
81 lines (62 loc) · 1.77 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef int TP;
const int MAXN = 1e3 + 5;
const TP INF = 0x3f3f3f3f;
TP matrix[MAXN][MAXN];
TP row[MAXN], col[MAXN];
int match[MAXN], way[MAXN];
TP hungarian(int n, int m){
memset(row, 0, sizeof row);
memset(col, 0, sizeof col);
memset(match, 0, sizeof match);
for(int i=1; i<=n; i++){
match[0] = i;
int j0 = 0, j1, i0;
TP delta;
vector<TP> minv (m+1, INF);
vector<bool> used (m+1, false);
do {
used[j0] = true;
i0 = match[j0];
j1 = -1;
delta = INF;
for(int j=1; j<=m; j++)
if(!used[j]){
TP cur = matrix[i0][j] - row[i0] - col[j];
if( cur < minv[j] ) minv[j] = cur, way[j] = j0;
if(minv[j] < delta) delta = minv[j], j1 = j;
}
for(int j=0; j<=m; j++)
if(used[j]){
row[match[j]] += delta,
col[j] -= delta;
}
else minv[j] -= delta;
j0 = j1;
} while(match[j0]);
do {
j1 = way[j0];
match[j0] = match[j1];
j0 = j1;
} while(j0);
}
return -col[0];
}
vector<pair<int, int>> getAssignment(int m){
vector<pair<int, int>> ans;
for(int i=1; i<=m; i++)
ans.push_back(make_pair(match[i], i));
return ans;
}
/*LATEX_DESC_BEGIN***************************
**Hungarian Algorithm** - Assignment Problem
Algoritmo para o problema de atribuição mínima.
**Complexity:** O(N^2 * M)
hungarian(int n, int m); -> Retorna o valor do custo mínimo
getAssignment(int m) -> Retorna a lista de pares <linha, Coluna> do Minimum Assignment
n -> Número de Linhas // m -> Número de Colunas
IMPORTANTE! O algoritmo é 1-indexado
IMPORTANTE! O tipo padrão está como int, para mudar para outro tipo altere | typedef <TIPO> TP; |
Extra: Para o problema da atribuição máxima, apenas multiplique os elementos da matriz por -1
*****************************LATEX_DESC_END*/