[P1361] 小M的作物 - 最小割
Description
两块巨大的耕地A和B,有n种作物的种子每种作物的种子有1个,第i种作物种植在A中种植可以获得ai的收益,在B中种植可以获得bi的收益。共有m种作物组合,第i个组合中的作物共同种在A中可以获得c1i的额外收益,共同总在B中可以获得c2i的额外收益。求种植的最大收益。
Solution
经典模型
基础部分:每个作物建点,S 向作物连边,边权为 bi,如果割了这条边表示这个作物种在 A 内;作物向 T 连边,边权为 ai,如果割了这条边表示这个作物种在 B 内
因为任意一条 S->T 路径上至少有一条边会被割掉,而这里每条路径上都恰好只有两条边,所以就相当于每个作物会选择自己在 A 内(割与 S 相连的边)或者选择自己在 B 内
扩展部分:对每个组合建立两个点 G,G',S->G 边权为 c2i,如果割掉这条边表示这个组合不全在 A 内;G'->T 边权为 c1i,如果割掉这条边表示这个组合不全在 B 内;G->gi,gi->G',边权为无穷,这些边不可被割
称 S->G 为 g, G'->T 为 g'
现在假设 g 不被割去,那么 S->g->G->infty->gi,那么此时所有 gi->T 就必须被割去,同时 g' 也必须被割去,这就意味着,如果你要求这个组合在 A 内生效,那么任意一个元素都不能种在 B,同时这个组合在 B 内也是无效的。
这就说明,如果 g 不被割去,其对应的含义和约束是满足题意的,对 g' 也同理。如果被割去,那么点 G 可以视为不存在,直接转化为基础部分的情况。这样就很粗糙地证明了这个过程的正确性
#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifndef __FLOW_HPP__
#define __FLOW_HPP__
// v1.1 feat. edge query for Maxf
#include <bits/stdc++.h>
using namespace std;
#define int long long
namespace flowsolution
{
const int N = 100005;
const int M = 2500005;
const int inf = 1e+12;
struct MaxflowSolution
{
int *dis, ans, cnt = 1, s, t, *pre, *next, *head, *val;
MaxflowSolution()
{
cnt = 1;
dis = new int[N];
pre = new int[M];
next = new int[M];
head = new int[N];
val = new int[M];
fill(dis, dis + N, 0);
fill(pre, pre + M, 0);
fill(next, next + M, 0);
fill(head, head + N, 0);
fill(val, val + M, 0);
}
~MaxflowSolution()
{
delete[] dis;
delete[] pre;
delete[] next;
delete[] head;
delete[] val;
}
std::queue<int> q;
int make(int x, int y, int z)
{
pre[++cnt] = y, next[cnt] = head[x], head[x] = cnt, val[cnt] = z;
int ret = cnt;
pre[++cnt] = x, next[cnt] = head[y], head[y] = cnt;
return ret;
}
int get_value(int x)
{
return val[x];
}
bool bfs()
{
fill(dis, dis + N, 0);
q.push(s), dis[s] = 1;
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i = head[x]; i; i = next[i])
if (!dis[pre[i]] && val[i])
dis[pre[i]] = dis[x] + 1, q.push(pre[i]);
}
return dis[t];
}
int dfs(int x, int flow)
{
if (x == t || !flow)
return flow;
int f = flow;
for (int i = head[x]; i; i = next[i])
if (val[i] && dis[pre[i]] > dis[x])
{
int y = dfs(pre[i], min(val[i], f));
f -= y, val[i] -= y, val[i ^ 1] += y;
if (!f)
return flow;
}
if (f == flow)
dis[x] = -1;
return flow - f;
}
int solve(int _s, int _t)
{
s = _s;
t = _t;
ans = 0;
for (; bfs(); ans += dfs(s, inf))
;
return ans;
}
};
struct CostflowSolution
{
struct Edge
{
int p = 0, c = 0, w = 0, next = -1;
} * e;
int s, t, tans, ans, cost, ind, *bus, qhead = 0, qtail = -1, *qu, *vis, *dist;
CostflowSolution()
{
e = new Edge[M];
qu = new int[M];
bus = new int[N];
vis = new int[N];
dist = new int[N];
fill(qu, qu + M, 0);
fill(bus, bus + N, -1);
fill(vis, vis + N, 0);
fill(dist, dist + N, 0);
ind = 0;
}
~CostflowSolution()
{
delete[] e;
delete[] qu;
delete[] vis;
delete[] dist;
}
void graph_link(int p, int q, int c, int w)
{
e[ind].p = q;
e[ind].c = c;
e[ind].w = w;
e[ind].next = bus[p];
bus[p] = ind;
++ind;
}
void make(int p, int q, int c, int w)
{
graph_link(p, q, c, w);
graph_link(q, p, 0, -w);
}
int dinic_spfa()
{
qhead = 0;
qtail = -1;
fill(vis, vis + N, 0);
fill(dist, dist + N, inf);
vis[s] = 1;
dist[s] = 0;
qu[++qtail] = s;
while (qtail >= qhead)
{
int p = qu[qhead++];
vis[p] = 0;
for (int i = bus[p]; i != -1; i = e[i].next)
if (dist[e[i].p] > dist[p] + e[i].w && e[i].c > 0)
{
dist[e[i].p] = dist[p] + e[i].w;
if (vis[e[i].p] == 0)
vis[e[i].p] = 1, qu[++qtail] = e[i].p;
}
}
return dist[t] < inf;
}
int dinic_dfs(int p, int lim)
{
if (p == t)
return lim;
vis[p] = 1;
int ret = 0;
for (int i = bus[p]; i != -1; i = e[i].next)
{
int q = e[i].p;
if (e[i].c > 0 && dist[q] == dist[p] + e[i].w && vis[q] == 0)
{
int res = dinic_dfs(q, min(lim, e[i].c));
cost += res * e[i].w;
e[i].c -= res;
e[i ^ 1].c += res;
ret += res;
lim -= res;
if (lim == 0)
break;
}
}
return ret;
}
pair<int, int> solve(int _s, int _t)
{
s = _s;
t = _t;
ans = 0;
cost = 0;
while (dinic_spfa())
{
fill(vis, vis + N, 0);
ans += dinic_dfs(s, inf);
}
return make_pair(ans, cost);
}
};
} // namespace flowsolution
#endif
signed main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n + 2);
vector<int> b(n + 2);
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
int m;
cin >> m;
int total_fortune = 0;
int s = n + 2 * m + 1, t = n + 2 * m + 2;
int GROUP_L = n, GROUP_R = n + m;
flowsolution::MaxflowSolution flow;
for (int i = 1; i <= n; i++)
{
flow.make(s, i, b[i]);
flow.make(i, t, a[i]);
total_fortune += a[i] + b[i];
}
for (int i = 1; i <= m; i++)
{
int k, c1, c2, x;
cin >> k >> c1 >> c2;
total_fortune += c1 + c2;
flow.make(s, GROUP_L + i, c2);
flow.make(GROUP_R + i, t, c1);
for (int j = 1; j <= k; j++)
{
cin >> x;
flow.make(GROUP_L + i, x, 1e9);
flow.make(x, GROUP_R + i, 1e9);
}
}
cout << total_fortune - flow.solve(s, t) << endl;
}