无序依赖。
注意最后输出方案时,即要输出最后的最大权闭合子图。
最大权闭合子图即最后还与S连通的点。
最后与S连通的点d一定不等于0(or-1,看写法了
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF (1e9)
const ll maxn=115,maxm=30005;
ll m,n,cnt,S,T;
ll w[maxn],cost[maxn],head[maxn];
struct node{
ll to,ne,w;
}e[maxm];
void add(ll u,ll v,ll w){
e[cnt]=(node){v,head[u],w};head[u]=cnt++;
e[cnt]=(node){u,head[v],0};head[v]=cnt++;
}
queue<ll>q;
ll d[maxn];
ll bfs(){
memset(d,0,sizeof(d));
while(!q.empty()) q.pop();
q.push(S);
d[S]=1;
while(!q.empty()){
ll u=q.front();q.pop();
if(u==T) break;//优化
for(ll i=head[u];~i;i=e[i].ne){
ll v=e[i].to;
if(!e[i].w) continue;
if(!d[v]) d[v]=d[u]+1,q.push(v);
}
}
return d[T];
}
ll dfs(ll u,ll remain){
if(u==T) return remain;
ll use=0;
for(ll &i=head[u];~i;i=e[i].ne){
ll v=e[i].to;
if(e[i].w&&remain&&d[v]==d[u]+1){
ll flow=dfs(v,min(remain,e[i].w));
use+=flow;remain-=flow;e[i].w-=flow;e[i^1].w+=flow;
}
}
if(!use) d[u]=0;//优化
return use;
}
ll rechead[maxn];
ll dinic(){
ll ans=0;
memcpy(rechead,head,sizeof(head));
while(bfs()){
while(1){
ll tmp=dfs(S,INF);
if(!tmp) break;
ans+=tmp;
}
memcpy(head,rechead,sizeof(head));
}
return ans;
}
int main(){
scanf("%lld%lld",&m,&n);
memset(head,-1,sizeof(head));
S=m+n+1;T=S+1;
ll x,sum=0;
for(ll i=1;i<=m;i++){
scanf("%lld",&w[i]);
sum+=w[i];
add(S,i,w[i]);
while(getchar()==' '){
scanf("%lld",&x);
add(i,x+m,INF);
}
}
for(ll i=1;i<=n;i++){
scanf("%lld",&x);
add(i+m,T,x);
}
sum-=dinic();
memcpy(head,rechead,sizeof(head));
//求方案。选择的仪器必在最大权闭合子图内,故一定与S连通,所以d一定不是0
for(ll i=1;i<=m+n;i++){
if(d[i]){
printf("%lld ",i>m?i-m:i);
}
if(i==m||i==n+m) printf("\n");
}
printf("%lld\n",sum);
return 0;
}