BZOJ2424 [HAOI2010]订货 - 费用流
题解
(非常裸的费用流
题意有一点表明不清: 该月卖出的商品可以不用算进仓库里面。
然后套上费用流模板
代码
1 #include<cstring> 2 #include<queue> 3 #include<cstdio> 4 #include<algorithm> 5 #define rd read() 6 #define rep(i,a,b) for(int i = (a); i <= (b); ++i) 7 using namespace std; 8 9 const int N = 1e4; 10 const int inf = 1061109567; 11 12 int n, m, maxin; 13 int nd[N], cost[N], head[N], tot, S, T = N - 1, maxflow, minco; 14 int dis[N], pre[N], vis[N]; 15 16 queue<int> q; 17 18 struct edge { 19 int nxt, val, to, c; 20 }e[N]; 21 22 int read() { 23 int X = 0, p = 1; char c = getchar(); 24 for(; c > '9' || c < '0'; c = getchar()) if(c == '-') p = -1; 25 for(; c >= '0' && c <= '9'; c = getchar()) X = X * 10 + c - '0'; 26 return X * p; 27 } 28 29 int ch(int x) { 30 return ( (x + 1) ^ 1) - 1; 31 } 32 33 void added(int fr, int to, int val, int c) { 34 e[++tot].to = to; 35 e[tot].val = val; 36 e[tot].c = c; 37 e[tot].nxt = head[fr]; 38 head[fr] = tot; 39 } 40 41 void add(int fr, int to, int val, int c) { 42 added(fr, to, val, c); 43 added(to, fr, 0, -c); 44 } 45 46 int bfs() { 47 memset(dis, 63, sizeof(dis)); 48 memset(vis, 0, sizeof(vis)); 49 memset(pre, 0, sizeof(pre)); 50 dis[S] = 0; 51 vis[S] = 1; 52 q.push(S); 53 for(int u, nt; !q.empty(); ) { 54 u = q.front(); q.pop(); 55 for(int i = head[u]; i; i = e[i].nxt ) { 56 nt = e[i].to; 57 if(dis[nt] <= dis[u] + e[i].c || !e[i].val) continue; 58 dis[nt] = dis[u] + e[i].c; 59 pre[nt] = i; 60 if(!vis[nt]) vis[nt] = 1, q.push(nt); 61 } 62 vis[u] = 0; 63 } 64 return dis[T]; 65 } 66 67 void EK() { 68 for(; bfs() != inf; ) { 69 int tmp = inf; 70 for(int i = pre[T]; i; i = pre[e[ch(i)].to]) tmp = min(tmp, e[i].val); 71 for(int i = pre[T]; i; i = pre[e[ch(i)].to]) e[i].val -= tmp, e[ch(i)].val += tmp; 72 maxflow += tmp; 73 minco += tmp * dis[T]; 74 } 75 } 76 77 int main() 78 { 79 n = rd; m = rd; maxin = rd; 80 rep(i, 1, n) nd[i] = rd; 81 rep(i, 1, n) cost[i] = rd; 82 rep(i, 1, n) { 83 add(S, i, inf, cost[i]); 84 add(i, T, nd[i], 0); 85 add(i, i + 1, maxin, m); 86 } 87 EK(); 88 printf("%d\n", minco); 89 }