Link
https://jzoj.net/senior/#main/show/1900
Preface
菜哭,菜的可怜,菜的真实。
Problem
类似NOI2006最大获利
Solution
朴素的做法是这样的:
答案可以写成 ∑ i , j a i a j b i , j − ∑ i a i c i \sum_{i,j}a_ia_jb_{i,j}-\sum_{i}a_ic_i i,j∑aiajbi,j−i∑aici
先考虑把所有的 ∑ i , j b i , j \sum_{i,j}b_{i,j} ∑i,jbi,j加上,然后现在考虑构一个最小割图。
左边一排点,向源点连 c i c_i ci的边权,表示如果割了则 a i = 1 a_i=1 ai=1,右边一排点,表示 ( i , j ) (i,j) (i,j),向汇点连边权为 b i , j b_{i,j} bi,j,注意到,如果某个 a i = 0 ∣ a j = 0 a_i=0 | a_j=0 ai=0∣aj=0,则 b i , j b_{i,j} bi,j要被减去,否则 c i , c j c_i,c_j ci,cj都被减去。
正好符合我们想要的割图。
优化是这样的:
不把一条边当作一个点,而是把一些边当作一个点。
那么, i i i与源点连 ∑ j b i , j \sum_{j}b_{i,j} ∑jbi,j,表示如果你 a i = 0 a_i=0 ai=0,则这些收益没有了。注意到我们还要减去 ∑ j b j , i \sum_{j}b_{j,i} ∑jbj,i,所以我们要对于每个点 j j j,向 i i i连一条 b j , i b_{j,i} bj,i的边,表示要把这些给割掉。当然,我们要把 i i i向汇点连 c i c_i ci表示 a i = 1 a_i=1 ai=1时你要付出的代价。
于是就是这样了。
Code
#include <bits/stdc++.h>
#define F(i, a, b) for (int i = a; i <= b; i ++)
const int M = 2e6;
const int N = 1e3 + 10;
const int inf = 2e9;
using namespace std;
int n, Ans, S, T, b[N][N], c[N], w[N], gap[N], dis[N], D[N];
int tov[M], nex[M], las[N], cur[N], len[M], tot;
void link(int x, int y, int c) {
tov[++ tot] = y, nex[tot] = las[x], las[x] = tot, len[tot] = c;
tov[++ tot] = x, nex[tot] = las[y], las[y] = tot, len[tot] = 0;
}
int dfs(int k, int flow) {
if (k == T)
return flow;
int have = 0;
for (int x = cur[k]; x ; x = nex[x]) {
cur[k] = x;
if (len[x] && dis[tov[x]] + 1 == dis[k]) {
D[++ D[0]] = tov[x];
int now = dfs(tov[x], min(flow - have, len[x]));
D[D[0] --] = 0;
len[x] -= now, len[x ^ 1] += now, have += now;
if (flow == have)
return have;
}
}
cur[k] = las[k];
if (!(-- gap[dis[k]])) dis[S] = T + 1;
++ gap[++ dis[k]];
return have;
}
int main() {
scanf("%d", &n);
F(i, 1, n)
F(j, 1, n)
scanf("%d", &b[i][j]), Ans += b[i][j], w[i] += b[i][j];
F(i, 1, n)
scanf("%d", &c[i]);
S = 0, T = n + 1, tot = 1;
F(i, 1, n)
link(S, i, w[i]), link(i, T, c[i]);
F(i, 1, n)
F(j, 1, n)
if (i ^ j)
link(j, i, b[j][i]);
gap[0] = T + 1;
while (dis[S] < T + 1)
Ans -= dfs(S, inf);
printf("%d\n", Ans);
}