-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10C.cpp
114 lines (100 loc) · 2.54 KB
/
10C.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
//
// Created by 91342 on 2018/12/11.
//
#include <iostream>
#include <cmath>
#include <cstring>
#include <assert.h>
#define N 210
#define MAX 3000
using namespace std;
int n, m, num;
int map[N][N] = {};
int dist[N][N] = {};
bool S[N] = {}, T[N] = {};
int match[N] = {};
int ls[N] = {}, lt[N] = {};
bool find(int i) {
S[i] = true;
for (int j = 1; j <= num; ++j) {
if (!T[j] && ls[i] + lt[j] == map[i][j]) {
T[j] = true;
if (!match[j] || find(match[j])) {
match[j] = i;
return true;
}
}
}
return false;
}
void relax() {
int relax_val = 1e9;
for(int i = 1; i <= min(n,m); ++i)
if(S[i]) {
for(int j = 1; j <= num; ++j)
if(!T[j] && ls[i] + lt[j] > map[i][j]) relax_val = min(relax_val, ls[i] + lt[j] - map[i][j]);
}
assert(relax_val > 0);
for (int i = 1; i <= num; ++i) {
if (S[i]) ls[i] -= relax_val;
if (T[i]) lt[i] += relax_val;
}
}
int KM() {
memset(match, 0, sizeof(match));
for (int i = 1; i <= min(m,n); ++i) {
ls[i] = 0;
for (int j = 1; j <= num; ++j) ls[i] = max(map[i][j], ls[i]);
}
for(int i = 1; i <= num; ++i) lt[i] = 0;
for (int i = 1; i <= min(n,m); ++i) {
while (1) {
memset(S, 0, sizeof(S));
memset(T, 0, sizeof(T));
if (find(i)) break;
else relax();
}
}
}
struct node {
int x, y;
};
node lost_mouse[N] = {};
node lost_kbd[N] = {};
int main() {
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
lost_mouse[i].x = x;
lost_mouse[i].y = y;
}
for (int i = 1; i <= n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
lost_kbd[i].x = x;
lost_kbd[i].y = y;
}
//理想情况:n >= m
num = max(m, n);
if (m > n) {
swap(m, n);
for (int i = 1; i <= m; ++i) swap(lost_mouse[i], lost_kbd[i]);
for(int i = m + 1; i <= num; ++i) {
lost_kbd[i] = lost_mouse[i];
lost_mouse[i] = {0,0};
}
}
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j) {
dist[i][j] = abs(lost_mouse[i].x - lost_kbd[j].x) + abs(lost_mouse[i].y - lost_kbd[j].y);
map[i][j] = MAX - dist[i][j];
//求最小权匹配,因此全部取负
}
KM();
int ans = 0;
for (int i = 1; i <= num; ++i)
if(match[i]) ans += dist[match[i]][i];
printf("%d\n", ans);
return 0;
}