WA 的点全部是方案错误。
玄关。
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
#define fi first
#define se second
int n,m,s,t;
ll ans;
vector<int> ans1,ans2;
set<int> st1,st2;
struct enode {
int nxt,to;
ll val;
} edge[500005];
int tot=1,head[105];
void add(int u,int v,ll w) {
edge[++tot]={head[u],v,w};
head[u]=tot;
}
int dep[105],now[105];
bool bfs() {
for(int i=s;i<=t;i++) dep[i]=1e9;
dep[s]=0;
now[s]=head[s];
queue<int> q;q.push(s);
while(!q.empty()) {
int u=q.front();q.pop();
for(int i=head[u];i;i=edge[i].nxt) {
int v=edge[i].to;ll w=edge[i].val;
if(w>0&&dep[v]==1e9) {
dep[v]=dep[u]+1;
now[v]=head[v];
if(v==t) return 1;
q.push(v);
}
}
}
return 0;
}
ll dfs(int u,ll sum) {
if(u==t) return sum;
ll k,flow=0;
for(int i=now[u];i&&sum>0;i=edge[i].nxt) {
now[u]=i;
int v=edge[i].to;ll w=edge[i].val;
if(w>0&&dep[v]==dep[u]+1) {
k=dfs(v,min(sum,w));
if(k==0) dep[v]=1e9;
edge[i].val-=k,edge[i^1].val+=k;
flow+=k,sum-=k;
}
}
return flow;
}
void bfs2() {
queue<int> q;q.push(s);
while(!q.empty()) {
int u=q.front();q.pop();
for(int i=head[u];i;i=edge[i].nxt) {
if(i&1) continue;
int v=edge[i].to;ll w=edge[i].val;
if(w>0) {
q.push(v);
if(u==s) st1.insert(v);
}
else {
if(v==t) st2.insert(u-m);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>m>>n;
s=0,t=n+m+1;
for(int i=1;i<=m;i++) {
int x;cin>>x;
ans+=x;
add(s,i,x);add(i,s,0);
string str;getline(cin,str);
x=0;
for(int j=0;j<str.length();j++) {
if(!isdigit(str[j])) {
if(x) {
add(i,m+x,1e18);add(m+x,i,0);
x=0;
}
}
else x=x*10+str[j]-'0';
}
if(x) {
add(i,m+x,1e18);add(m+x,i,0);
x=0;
}
}
for(int i=1;i<=n;i++) {
int x;cin>>x;
add(m+i,t,x);add(t,m+i,0);
}
while(bfs()) ans-=dfs(s,1e18);
bfs2();
for(auto x:st1) cout<<x<<" ";
cout<<"\n";
for(auto x:st2) cout<<x<<" ";
cout<<"\n"<<ans<<"\n";
return 0;
}