最裸的最大流,没啥好说的。。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = + ;
const int M = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int n, m, tot, S, T, sum, head[N << ], level[N << ], a[N << ], b[N << ]; struct node {
int u, v, w, nx;
} edge[M << ]; void add(int u, int v, int w) {
edge[tot].u = u; edge[tot].v = v; edge[tot].w = w;
edge[tot].nx = head[u]; head[u] = tot++;
} bool bfs() {
memset(level, , sizeof(level));
queue<int> que;
level[S] = ; que.push(S); while(!que.empty()) {
int u = que.front(); que.pop();
if(u == T) return true; for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].v;
if(level[v] || edge[i].w <= ) continue;
level[v] = level[u] + ;
que.push(v);
}
}
return false;
} int dfs(int u, int p) {
if(u == T) return p;
int ret = ;
for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].v, w = edge[i].w;
if(level[v] != level[u] + || w <= ) continue;
int f = dfs(v, min(p - ret, w));
ret += f;
edge[i].w -= f;
edge[i ^ ].w += f;
if(ret == p) break;
}
if(!ret) level[u] = ;
return ret;
} int Dinic() { int ans = ;
while(bfs()) ans += dfs(S, inf);
return ans;
} void init() {
tot = ;
sum = ;
memset(head, -, sizeof(head));
} int main(){
int cas; scanf("%d", &cas);
while(cas--) {
init();
scanf("%d%d", &n, &m); S = , T = n + m + ;
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
add(i, T, a[i]);
add(T, i, );
sum += a[i];
} for(int i = ; i <= m; i++) {
scanf("%d", &b[i]);
add(S, i + n, b[i]);
add(i + n, S, );
} for(int i = ; i <= m; i++) {
int k; scanf("%d", &k);
for(int j = ; j <= k; j++) {
int id; scanf("%d", &id);
add(i + n, id, inf);
add(id, i + n, );
}
} int ans = Dinic();
printf("%d\n", sum - ans);
}
return ;
} /*
*/