传送门
我就是抄的题解,具体还是看题解。
这题输入坑。。
话说我好像没用题解的isap,我用的dinic
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#define ll long long
using namespace std;
inline int read(){
int x=0;char ch=' ';int f=1;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x*f;
}
const int N=51,M=51;
struct edge{int to,next,cap,flow;}e[N<<5],e2[N<<5];
int n,m,s,t,tot=-1,sum;
int head[N<<4],d[N<<4],q[N<<4],val[N<<4],cost[N<<4],cur[N<<4];
inline void addedge(int x,int y,int c){
e[++tot].to=y;e[tot].next=head[x];e[tot].cap=c;e[tot].flow=0;head[x]=tot;
e[++tot].to=x;e[tot].next=head[y];e[tot].cap=0;e[tot].flow=0;head[y]=tot;
}
inline bool bfs(){
for(int i=s;i<=t;i++)d[i]=0x3f3f3f3f;
d[s]=0;int l=1,r=1;q[1]=s;
while(l<=r){
int x=q[l++];
for(int i=head[x];i!=-1;i=e[i].next){
int u=e[i].to;
if(e[i].cap>e[i].flow&&d[u]>d[x]+1){
d[u]=d[x]+1;
q[++r]=u;
}
}
}
return d[t]!=0x3f3f3f3f;
}
inline int dfs(int x,int a){
if(x==t||!a)return a;
int flow=0,f;
for(int &i=cur[x];i!=-1;i=e[i].next){
int u=e[i].to;
if(e[i].cap>e[i].flow&&d[u]==d[x]+1&&(f=dfs(u,min(a,e[i].cap-e[i].flow)))>0){
e[i].flow+=f;
e[i^1].flow-=f;
a-=f;
flow+=f;
if(!a)return flow;
}
}
return flow;
}
inline int dinic(){
int flow=0;
while(bfs()){
for(int i=s;i<=t;i++)cur[i]=head[i];
flow+=dfs(s,0x3f3f3f3f);
}
return flow;
}
char c[10010];
vector<int> req[N<<4];
bool can1[N<<4],can2[N<<4];
inline void init(){
s=1,t=2+n+m;
for(int i=1;i<=n;i++){
addedge(s,i+1,val[i]);
for(int j=0;j<(int)req[i].size();j++)addedge(i+1,1+n+req[i][j],0x3f3f3f3f+1);
}
for(int i=1;i<=m;i++)addedge(i+n+1,t,cost[i]);
}
int main(){
memset(head,-1,sizeof(head));
n=read();m=read();
int num;
for (int i=1;i<=n;i++){
val[i]=read();
sum+=val[i];
memset(c,0,sizeof(c));
cin.getline(c,10000);
int ulen=0;
while(sscanf(c+ulen,"%d",&num)==1){
req[i].push_back(num);
if(num==0)ulen++;
else while(num){
num/=10;
ulen++;
}
ulen++;
}
}
for(int i=1;i<=m;i++)cost[i]=read();
init();
for(int i=0;i<=tot;i++)e2[i].cap=e[i].cap,e2[i].flow=e[i].flow;
int ans=dinic();
for(int i=head[t];i!=-1;i=e[i].next){
for(int j=0;j<=tot;j++)e[j].cap=e2[j].cap,e[j].flow=e2[j].flow;
int num=e[i^1].cap;
e[i^1].cap=0;
int tmp=dinic();
if(ans-tmp==num){can1[e[i].to-n-1]=true;}
e[i^1].cap=num;
}
for(int i=1;i<=n;i++){
can2[i]=true;
for(int j=0;j<(int)req[i].size();j++)
if(!can1[req[i][j]]){
can2[i]=false;
break;
}
}
for(int i=1;i<=n;i++)if(can2[i])printf("%d ",i);
printf("\n");
for(int i=1;i<=m;i++)if(can1[i])printf("%d ",i);
printf("\n");
printf("%d",sum-ans);
return 0;
}