最大权闭合子图裸题,不说了吧,求方案就是把s集遍历一遍。
错误记录:dfs那块忘判断残量了,11分×1.
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 typedef long long ll; 7 template<typename T>inline char MIN(T&A,T B){return A>B?A=B,1:0;} 8 template<typename T>inline char MAX(T&A,T B){return A<B?A=B,1:0;} 9 template<typename T>inline T _min(T A,T B){return A<B?A:B;} 10 template<typename T>inline T _max(T A,T B){return A>B?A:B;} 11 template<typename T>inline void inc(T&A,T B){A+=B;} 12 inline int read(int&x){ 13 x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1; 14 while(isdigit(c))x=x*10+c-'0',c=getchar();if(c==' ')return -1;return f?-x:x; 15 } 16 const int N=100+7,M=10000+7,INF=0x3f3f3f3f; 17 int Head[N],cur[N],Next[M<<1],v[M<<1],w[M<<1],dis[N],vis[N],tot=1,n,m,cnt,s,t; 18 19 inline void Addedge(int x,int y,int z){ 20 v[++tot]=y,Next[tot]=Head[x],Head[x]=tot,w[tot]=z; 21 v[++tot]=x,Next[tot]=Head[y],Head[y]=tot,w[tot]=0; 22 } 23 #define y v[j] 24 inline int bfs(){ 25 queue<int> q;memset(dis,0,sizeof dis),dis[s]=1,q.push(s); 26 for(register int i=1;i<=(n+m)+2;++i)cur[i]=Head[i]; 27 while(!q.empty()){ 28 int x=q.front();q.pop(); 29 for(register int j=Head[x];j;j=Next[j])if(w[j]&&!dis[y]){ 30 dis[y]=dis[x]+1,q.push(y); 31 if(y==t)return 1; 32 } 33 } 34 return 0; 35 } 36 int dinic(int x,int flow){ 37 if(!flow||x==t)return flow; 38 int rest=flow,k; 39 for(register int j=cur[x];j&&rest;cur[x]=j,j=Next[j])if(w[j]&&dis[y]==dis[x]+1){ 40 if(!(k=dinic(y,_min(rest,w[j]))))dis[y]=0; 41 rest-=k,w[j]-=k,w[j^1]+=k; 42 } 43 return flow-rest; 44 } 45 #undef y 46 int x,y,z,maxflow;ll ans; 47 void dfs(int x){ 48 vis[x]=1;for(register int j=Head[x];j;j=Next[j])if(w[j]&&!vis[v[j]])dfs(v[j]); 49 } 50 51 int main(){//freopen("P2762.in","r",stdin);//freopen("tmp.out","w",stdout); 52 read(m),read(n);s=n+m+1,t=s+1; 53 for(register int i=1;i<=m;++i){ 54 ans+=read(z),Addedge(s,i,z);while(~read(y))Addedge(i,y+m,INF); Addedge(i,y+m,INF); 55 } 56 for(register int i=1;i<=n;++i)read(z),Addedge(i+m,t,z); 57 while(bfs())maxflow+=dinic(s,INF);//printf("%lld %d ",ans,maxflow); 58 ans-=maxflow,dfs(s); 59 for(register int i=1;i<=m;++i)if(vis[i])printf("%d ",i);puts(""); 60 for(register int i=1;i<=n;++i)if(vis[i+m])printf("%d ",i);puts(""); 61 printf("%d ",ans); 62 return 0; 63 }