题解

(非常裸的费用流

题意有一点表明不清: 该月卖出的商品可以不用算进仓库里面。

然后套上费用流模板

代码

 #include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
#define rd read()
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
using namespace std; const int N = 1e4;
const int inf = ; int n, m, maxin;
int nd[N], cost[N], head[N], tot, S, T = N - , maxflow, minco;
int dis[N], pre[N], vis[N]; queue<int> q; struct edge {
int nxt, val, to, c;
}e[N]; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} int ch(int x) {
return ( (x + ) ^ ) - ;
} void added(int fr, int to, int val, int c) {
e[++tot].to = to;
e[tot].val = val;
e[tot].c = c;
e[tot].nxt = head[fr];
head[fr] = tot;
} void add(int fr, int to, int val, int c) {
added(fr, to, val, c);
added(to, fr, , -c);
} int bfs() {
memset(dis, , sizeof(dis));
memset(vis, , sizeof(vis));
memset(pre, , sizeof(pre));
dis[S] = ;
vis[S] = ;
q.push(S);
for(int u, nt; !q.empty(); ) {
u = q.front(); q.pop();
for(int i = head[u]; i; i = e[i].nxt ) {
nt = e[i].to;
if(dis[nt] <= dis[u] + e[i].c || !e[i].val) continue;
dis[nt] = dis[u] + e[i].c;
pre[nt] = i;
if(!vis[nt]) vis[nt] = , q.push(nt);
}
vis[u] = ;
}
return dis[T];
} void EK() {
for(; bfs() != inf; ) {
int tmp = inf;
for(int i = pre[T]; i; i = pre[e[ch(i)].to]) tmp = min(tmp, e[i].val);
for(int i = pre[T]; i; i = pre[e[ch(i)].to]) e[i].val -= tmp, e[ch(i)].val += tmp;
maxflow += tmp;
minco += tmp * dis[T];
}
} int main()
{
n = rd; m = rd; maxin = rd;
rep(i, , n) nd[i] = rd;
rep(i, , n) cost[i] = rd;
rep(i, , n) {
add(S, i, inf, cost[i]);
add(i, T, nd[i], );
add(i, i + , maxin, m);
}
EK();
printf("%d\n", minco);
}
05-11 21:45