yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P4014 分配问题

P4014 分配问题

题面

题目链接

解法

不就是个二分图最小/最大权匹配吗,就这也能紫题?

AC代码

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
/**
* @file: P4014.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P4014
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp& x) {
bool sign = false;
char ch = getchar();
long double tmp = 1;
for (; !isdigit(ch); ch = getchar())
sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch == '.')
for (ch = getchar(); isdigit(ch); ch = getchar())
tmp /= 10.0, x += tmp * (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar((x % 10) ^ 48);
}
template <const int MAXV, const int MAXE>
struct MINCMF {
const int INF = 0x3f3f3f3f3f3f3f3f;
struct Edge {
int v, flow, cost;
} edge[MAXE * 2];
int tot = 1, head[MAXV], nxt[MAXE];
int flow, cost, cur[MAXV], dis[MAXV];
bool vis[MAXV];
void addedge(int u, int v, int flow, int cost) {
edge[++tot] = {v, flow, cost};
nxt[tot] = head[u], head[u] = tot;
edge[++tot] = {u, 0, -cost};
nxt[tot] = head[v], head[v] = tot;
}
bool spfa(int s, int t) {
fill(vis, vis + MAXV, 0);
fill(dis, dis + MAXV, INF);
queue<int> que;
que.push(s);
dis[s] = 0;
vis[s] = 1;
while (!que.empty()) {
int u = que.front();
que.pop();
vis[u] = 0;
for (int i = head[u]; i; i = nxt[i]) {
int v = edge[i].v;
if (edge[i].flow && dis[v] > dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
if (!vis[v]) {
que.push(v);
vis[v] = 1;
}
}
}
}
return dis[t] != INF;
}
int augment(int u, int t, int mx) {
if (u == t || mx == 0)
return mx;
vis[u] = 1;
int ret = 0;
for (int &i = cur[u]; i; i = nxt[i]) {
int v = edge[i].v;
if (vis[v] || dis[v] != dis[u] + edge[i].cost)
continue;
int tmp = augment(v, t, min(mx, edge[i].flow));
cost += tmp * edge[i].cost;
mx -= tmp, ret += tmp;
edge[i].flow -= tmp, edge[i ^ 1].flow += tmp;
if (mx == 0)
break;
}
vis[u] = 0;
return ret;
}
pair<int, int> mcmf(int s, int t) {
while (spfa(s, t)) {
copy(head, head + MAXV, cur);
flow += augment(s, t, INF);
}
return make_pair(flow, cost);
}
};
template <const int MAXV, const int MAXE>
struct MAXCMF {
const int INF = 0x3f3f3f3f3f3f3f3f;
struct Edge {
int v, flow, cost;
} edge[MAXE * 2];
int tot = 1, head[MAXV], nxt[MAXE];
int flow, cost, cur[MAXV], dis[MAXV];
bool vis[MAXV];
void addedge(int u, int v, int flow, int cost) {
edge[++tot] = {v, flow, cost};
nxt[tot] = head[u], head[u] = tot;
edge[++tot] = {u, 0, -cost};
nxt[tot] = head[v], head[v] = tot;
}
bool spfa(int s, int t) {
fill(vis, vis + MAXV, 0);
fill(dis, dis + MAXV, -INF);
queue<int> que;
que.push(s);
dis[s] = 0;
vis[s] = 1;
while (!que.empty()) {
int u = que.front();
que.pop();
vis[u] = 0;
for (int i = head[u]; i; i = nxt[i]) {
int v = edge[i].v;
if (edge[i].flow && dis[v] < dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
if (!vis[v]) {
que.push(v);
vis[v] = 1;
}
}
}
}
return dis[t] != -INF;
}
int augment(int u, int t, int mx) {
if (u == t || mx == 0)
return mx;
vis[u] = 1;
int ret = 0;
for (int &i = cur[u]; i; i = nxt[i]) {
int v = edge[i].v;
if (vis[v] || dis[v] != dis[u] + edge[i].cost)
continue;
int tmp = augment(v, t, min(mx, edge[i].flow));
cost += tmp * edge[i].cost;
mx -= tmp, ret += tmp;
edge[i].flow -= tmp, edge[i ^ 1].flow += tmp;
if (mx == 0)
break;
}
vis[u] = 0;
return ret;
}
pair<int, int> mcmf(int s, int t) {
while (spfa(s, t)) {
copy(head, head + MAXV, cur);
flow += augment(s, t, INF);
}
return make_pair(flow, cost);
}
};
const int MAXN = 1e3 + 10;
const int MAXV = 2e3 + 10;
const int MAXE = 2e6 + 10;
const int INF = 0x3f3f3f3f3f3f3f3f;
int n, s, t, num, a[MAXN][MAXN], pt[MAXN][2];
MINCMF<MAXV, MAXE> minnt;
MAXCMF<MAXV, MAXE> maxnt;
signed main() {
read(n);
s = ++num, t = ++num;
for (int i = 1; i <= n; ++i) {
pt[i][0] = ++num, pt[i][1] = ++num;
for (int j = 1; j <= n; ++j) {
read(a[i][j]);
}
}
for (int i = 1; i <= n; ++i) {
minnt.addedge(s, pt[i][0], 1, 0);
maxnt.addedge(s, pt[i][0], 1, 0);
minnt.addedge(pt[i][1], t, 1, 0);
maxnt.addedge(pt[i][1], t, 1, 0);
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
minnt.addedge(pt[i][0], pt[j][1], 1, a[i][j]);
maxnt.addedge(pt[i][0], pt[j][1], 1, a[i][j]);
}
}
write(minnt.mcmf(s, t).second), putchar('\n');
write(maxnt.mcmf(s, t).second), putchar('\n');
return 0;
}