https://www.luogu.org/problemnew/show/P2762
最大权闭合子图知识点看这里:https://www.cnblogs.com/dilthey/p/7565206.html
#include <bits/stdc++.h>
using namespace std;
const int maxn=105;
const int INF=0x3f3f3f3f;
struct Edge{
int from,to,cap,flow;
};
struct Dinic{
int n1,n2,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int cost[maxn],sum=0;
void init()
{
char str[10000];
int tool,len;
cin>>n1>>n2;
s=0;t=n1+n2+1;
for(int i=1;i<=n1;i++)
{
len=0;
scanf("%d ",&cost[i]);
sum+=cost[i];
gets(str);
while(sscanf(str+(len==0?len:len-1),"%d",&tool)==1)
{
AddEdge(i,n1+tool,INF);
while(tool)len++,tool/=10;
len++;
}
}
for(int i=1;i<=n2;i++)scanf("%d",&cost[n1+i]);
for(int i=1;i<=n1;i++)AddEdge(s,i,cost[i]);
for(int i=n1+1;i<=n1+n2;i++)AddEdge(i,t,cost[i]);
}
void AddEdge(int f,int t,int c)
{
edges.push_back((Edge){f,t,c,0});
edges.push_back((Edge){t,f,0,0});
m=edges.size();
G[f].push_back(m-2);
G[t].push_back(m-1);
}
bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty())
{
int x=Q.front();Q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t || a==0)return a;
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(!a)break;
}
}
return flow;
}
int MaxFlow()
{
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
void print(int flow)
{
for(int i=1;i<=n1;i++)
if(vis[i])printf("%d ",i);
putchar('\n');
for(int i=n1+1;i<=n1+n2;i++)
if(vis[i])printf("%d ",i-n1);
putchar('\n');
printf("%d\n",sum-flow);
}
}ans;
int main()
{
//freopen("input.in","r",stdin);
ans.init();
int flow=ans.MaxFlow();
ans.print(flow);
return 0;
}