题意
有$m$个实验,$n$中器材,每个实验需要使用一些器材
每个实验有收入,每个器材有花费
最大化收入 - 花费
Sol
最大权闭合图的经典应用
从$S$向每个实验连流量为该实验收入的边
从每个器材箱$T$连流量为花费的边
每个实验向其需要其器材连边权为$INF$的边
答案为:总收入 - 最小割
考虑如何统计方案
在最小割中,割去实验表示不选该实验。
那么我们从源点出发,不经过边权为$0$的边,走到的就是需要选的。
这正好是Dinic最后一次增光的deep数组
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN = 1e5 + , INF = 1e9 + ;
char c;
inline int read() {
if(c == '\r') return ;
c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int M, N, S, T;
struct Edge {
int u, v, f, nxt;
}E[MAXN];
int head[MAXN], cur[MAXN], num;
inline void add_edge(int x, int y, int f) {
E[num] = (Edge){x, y, f, head[x]};
head[x] = num++;
}
inline void AddEdge(int x, int y, int z) {
add_edge(x, y, z);
add_edge(y, x, );
}
int sum = , deep[MAXN];
bool BFS() {
queue<int> q; q.push(S);
memset(deep, , sizeof(deep)); deep[S] = ;
while(!q.empty()) {
int p = q.front(); q.pop();
for(int i = head[p]; i != -; i = E[i].nxt) {
int to = E[i].v;
if(!deep[to] && E[i].f) {
deep[to] = deep[p] + ;
q.push(to);
}
}
}
return deep[T] > ;
}
int DFS(int x, int flow) {
if(x == T) return flow;
int ansflow = ;
for(int &i = cur[x]; i != -; i = E[i].nxt) {
int to = E[i].v;
if(deep[to] == deep[x] + && E[i].f) {
int nowflow = DFS(to, min(flow, E[i].f));
E[i].f -= nowflow; E[i ^ ].f += nowflow;
ansflow += nowflow; flow -= nowflow;
if(flow <= ) break;
}
}
return ansflow;
}
int Dinic() {
int ans = ;
while(BFS()) {
memcpy(cur, head, sizeof(head));
ans += DFS(S, INF);
}
return ans;
}
int main() {
memset(head, -, sizeof(head));
scanf("%d %d\n", &M, &N); S = ; T = N + M + ;
int ans = ;
for(int i = ; i <= M; i++) {
c = '+';
int val = read(), x; ans += val;
AddEdge(S, i, val);
while(x = read())
AddEdge(i, x + M, INF);
}
for(int i = ; i <= N; i++) {
int x; scanf("%d", &x);
AddEdge(i + M, T, x);
}
int cut = Dinic();
for(int i = head[S]; i != -; i = E[i].nxt)
if(E[i].f)
printf("%d ", E[i].v); puts("");
for(int x = M + ; x <= N + M; x++)
for(int i = head[x]; i != -; i = E[i].nxt)
if(E[i].f)
{printf("%d ", x - M); break;}
puts("");
printf("%d", ans - cut);
return ;
}