CF724E Goods transportation 题解
Solution
首先不难想到网络流的做法,即每个点 \(S\to i\) 连 \(p_i\),\(i\to T\) 连 \(s_i\),然后 \(i\to j(i<j)\) 连 \(c\),跑最大流即可。
考虑到最大流等于最小割,所以我们考虑用 dp 去优化网络流。我们可以发现我们从前往后进行考虑,对于一个点 \(i\),我们考虑断掉的方案:
-
切掉前面所以没切掉 \(S\to j\) 的边与 \(i\) 之间的边以及 \(S\to i\) 的边
-
切掉 \(i\to T\) 的边。
然后我们可以设 \(f_{i,j}\) 表示前面 \(i\) 个数,有 \(j\) 个没有切掉与 \(S\) 相连的边的最小割。可以列出转移式:
\[f_{i,j}=\min(f_{i-1,j-1}+p_i,f_{i-1,j}+s_i+c\times j)
\]
Code
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define int long long
#define MAXN 10005
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}
int n,c,p[MAXN],s[MAXN],dp[MAXN];
signed main(){
read (n,c);
for (Int i = 1;i <= n;++ i) read (p[i]);
for (Int i = 1;i <= n;++ i) read (s[i]);
for (Int i = 1;i <= n;++ i){
dp[i] = dp[i - 1] + s[i];
for (Int j = i - 1;j >= 1;-- j) dp[j] = min (dp[j - 1] + s[i],dp[j] + p[i] + c * j);
dp[0] += p[i];
}
int ans = 1e18;
for (Int i = 0;i <= n;++ i) chkmin (ans,dp[i]);
write (ans),putchar ('\n');
return 0;
}