1 条题解
-
1HLBhahaqiu LV 8 MOD @ 2020-08-30 21:59:24
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <vector>typedef long long ll;
const int N = 1205;
const ll INF = 1e18;int read() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}ll min(ll x, ll y) {
if (x <= y) return x; return y;
}struct Edge {
int from, to; ll flow, cap, cost;
};
std::vector<Edge> edges;
std::vector<int> G[N];
int S, SS, T, n, p[N]; ll d[N], a[N];void addedge(int from, int to, ll cap, ll cost) {
edges.push_back((Edge){from, to, 0, cap, cost});
edges.push_back((Edge){to, from, 0, 0, -cost});
int size = edges.size();
G[from].push_back(size - 2);
G[to].push_back(size - 1);
}bool SPFA(ll &flow, ll &cost) {
bool inq[N] = {}; inq[S] = 1;
std::queue<int> Q; Q.push(S);
for (int i = 0; i < n; ++ i) d[i] = INF;
d[S] = p[S] = 0, a[S] = INF;
while (!Q.empty()) {
int u = Q.front(); Q.pop(), inq[u] = 0;
int size = G[u].size();
for (int i = 0; i < size; ++ i) {
Edge e = edges[G[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to]) Q.push(e.to), inq[e.to] = 1;
}
}
}
if (d[T] == INF) return false;
flow += a[T], cost += a[T] * d[T];
int x = T;
while (x != S) {
edges[p[x]].flow += a[T], edges[p[x]^1].flow -= a[T];
x = edges[p[x]].from;
}
return true;
}ll mincost() {
ll flow = 0, cost = 0;
while (SPFA(flow, cost)) {}
return cost;
}int main() {
n = read(); int k = read(); S = 0, SS = n * 6 + 1, T = SS + 1;
addedge(S, SS, k, 0);
for (int i = 1; i <= n * 3; ++ i) {
int x = read();
addedge(SS, i, 1, 0);
addedge(i, i + n * 3, 1, -x);
addedge(i + n * 3, T, 1, 0);
for (int j = i + n; j <= n * 3; ++ j)
addedge(i + n * 3, j, 1, 0);
}
n = T + 1;
printf("%lld\n", -mincost());
return 0;
}
- 1
信息
- ID
- 1056
- 难度
- 9
- 分类
- (无)
- 标签
- (无)
- 递交数
- 1
- 已通过
- 1
- 通过率
- 100%
- 上传者