裸的费用流。。。。
建图方法:把每个月抽象成一个点
S向每个点连边,费用为当月购进价,容量无限大;每个点向T连边,费用为0,容量为当月卖出量
每个点向后一个月的点连边,费用为仓库储存费用m,容量为仓库的容量s
1 /************************************************************** 2 Problem: 2424 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:0 ms 7 Memory:1028 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 13 using namespace std; 14 const int inf = (int) 1e9; 15 const int N = 2005; 16 const int M = 10005; 17 struct edges{ 18 int next, to, cost, f; 19 } e[M]; 20 int tot = 1, n, m, s; 21 int first[N], d[N], g[N], q[M]; 22 bool v[N]; 23 int S, T, ans; 24 25 inline int read(){ 26 int x = 0, sgn = 1; 27 char ch = getchar(); 28 while (ch < '0' || ch > '9'){ 29 if (ch == '-') sgn = -1; 30 ch = getchar(); 31 } 32 while (ch >= '0' && ch <= '9'){ 33 x = x * 10 + ch - '0'; 34 ch = getchar(); 35 } 36 return sgn * x; 37 } 38 39 inline void add_edge(int x, int y, int d, int c){ 40 e[++tot].next = first[x]; 41 first[x] = tot; 42 e[tot].to = y; 43 e[tot].f = d; 44 e[tot].cost = c; 45 } 46 47 void add_Edges(int x, int y,int d, int c){ 48 add_edge(x, y, d, c); 49 add_edge(y, x, 0, -c); 50 } 51 52 inline int calc(){ 53 int flow = inf; 54 for (int x = g[T]; x; x = g[e[x ^ 1].to]) 55 flow = min(flow, e[x].f); 56 for (int x = g[T]; x; x = g[e[x ^ 1].to]) 57 e[x].f -= flow, e[x ^ 1].f += flow; 58 return flow; 59 } 60 61 bool spfa(){ 62 for (int i = 1; i <= T; ++i) 63 d[i] = inf; 64 d[S] = 0, v[S] = 1; 65 q[1] = S; 66 int x, y; 67 for(int l = 1, r = 1; l <= r; ++l){ 68 for (x = first[q[l]]; x; x = e[x].next){ 69 y = e[x].to; 70 if (d[q[l]] + e[x].cost < d[y] && e[x].f){ 71 d[y] = d[q[l]] + e[x].cost; 72 g[y] = x; 73 if (!v[y]) 74 q[++r] = y, v[y] = 1; 75 } 76 } 77 v[q[l]] = 0; 78 } 79 return d[T] != inf; 80 } 81 82 int main(){ 83 n = read(), m = read(), s = read(); 84 S = n + 1, T = n + 2; 85 for (int i = 1; i <= n; ++i) 86 add_Edges(i, T, read(), 0); 87 for (int i = 1; i <= n; ++i) 88 add_Edges(S, i, inf, read()); 89 for (int i = 1; i < n; ++i) 90 add_Edges(i, i + 1, s, m); 91 while (spfa()) 92 ans += calc() * d[T]; 93 printf("%d\n", ans); 94 return 0; 95 }
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen