-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhungarian.cpp
More file actions
209 lines (170 loc) · 5.7 KB
/
hungarian.cpp
File metadata and controls
209 lines (170 loc) · 5.7 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
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
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<utility>
#include<stack>
#include<map>
#define MAX_V 1010
#define MAX_U 1010
#define MAX_UV 2020
#define INF 2e9
#define EPS 1e-7
#define min(a,b) a<b ? a : b
using namespace std;
typedef pair< double, pair<int,int> > edge;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
double c[MAX_V][MAX_U];
bool lessThan(double a, double b){
return (b - a) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * EPS);
}
class Hungarian{
public:
double a[MAX_V], b[MAX_V], x[MAX_V], minC;
int M[MAX_UV], parent[MAX_UV], visited[MAX_UV];
vector<int> eq[MAX_UV];
set<int> star;
int n, ci, cj;
Hungarian(){
scanf("%d",&n);
b[0] = INF;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
scanf("%lf",&c[i][j]);
if(lessThan(c[i][j],b[0])){
b[0] = c[i][j];
}
}
a[i] = 0;
eq[i].clear();
eq[i+n].clear();
M[i] = M[i+n] = -1;
}
for(int j=0; j<n; j++) b[j] = b[0];
}
void classic(void){
for(int k=1; k<=n; k++){
bool augment = false;
int last = -1;
while(!augment){
queue<int> trees;
for(int i=0; i<n; i++) eq[i].clear(), eq[i+n].clear();
for(int i=0; i<n; i++){
if(M[i] == -1){
//root nodes of hungarian trees
trees.push(i);
}
for(int j=0; j<n; j++){
if(fabs(c[i][j] - (a[i]+b[j])) < EPS){
//add admissible edges to equality subgraph
eq[i].push_back(n+j);
eq[n+j].push_back(i);
}
}
}
star.clear();
while(!trees.empty()){
int s = trees.front(); trees.pop();
for(int i=0; i<n; i++) { parent[i] = parent[i+n] = -1; visited[i] = visited[i+n] = 0; }
queue<int> q;
q.push(s);
while(!q.empty()){
//bfs to find an augmenting path
int node = q.front(); q.pop();
star.insert(node);
if(node >= n && M[node]==-1){
augment = true;
last = node;
}
if(!visited[node]){
for(int i=0; i<(int)eq[node].size(); i++){
int son = eq[node][i];
//search through alternating paths
if(!visited[son] && parent[node] != son && ((node<n && M[son]!=node)||(node>=n && M[son]==node))){
q.push(son);
parent[son] = node; //record the path
}
}
visited[node]=1;
}
}
if(augment) break;
}
if(!augment){
double lambda=INF;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(star.count(i) && !star.count(j+n)){
double diff = c[i][j] - a[i] - b[j];
if(lessThan(diff,lambda)){
lambda = diff;
}
}
}
}
for(int i=0; i<n; i++){
//change dual variables
a[i] += (star.count(i))? lambda : 0;
b[i] += (star.count(i+n))? -(lambda) : 0;
}
}
}
int node = last;
while(parent[node] >= 0){
//augment current matching
if(M[node]==-1){
M[node] = parent[node];
M[parent[node]] = node;
}
else{
M[parent[node]] = -1;
}
node = parent[node];
}
}
}
void printC(){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%.6lf%s", c[i][j], (j<n-1 ? "\t" : ""));
}
printf("\n");
}
}
void printAB(){
for(int i=0; i<n; i++){
printf("%lf%s", a[i], (i<n-1 ? "\t" : ""));
}
printf("\n");
for(int i=0; i<n; i++){
printf("%lf%s", b[i], (i<n-1 ? "\t" : ""));
}
printf("\n");
}
void printM(){
for(int i=0; i<n; i++){
printf("%d %d\n", i, M[i]-n);
}
}
double matchingValue(){
double sum = 0.0;
for(int i=0; i<n; i++)
if(M[i] != -1)
sum += c[i][M[i]-n];
return sum;
}
};
int main(){
Hungarian h;
h.classic();
h.printM();
printf("\n%lf\n",h.matchingValue());
return 0;
}