WA求助
查看原帖
WA求助
556362
Unnamed114514楼主2023/7/13 15:44
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,T,s,t,tot=1,a[1005],head[105],nxt[20005],now[105],to[20005],c[20005],dep[105],pre[1005];
queue<int> q;
inline bool bfs(int u){
	memset(dep,0,sizeof(dep));
	while(q.size())	q.pop();
	q.push(u),dep[u]=1;
	while(q.size()){
		int u=q.front();
		q.pop();
		now[u]=head[u];
		if(u==t)
			return 1;
		for(int t=head[u];t;t=nxt[t])
			if(!dep[to[t]]&&c[t]){
				dep[to[t]]=dep[u]+1;
				q.push(to[t]);
			}
	}
	return 0;
}
int dfs(int u,int F){
	if(u==t)
		return F;
	int rest=F,o,t;
	for(t=now[u];t&&rest;t=nxt[t])
		if(c[t]&&dep[to[t]]==dep[u]+1){
			o=dfs(to[t],min(F,c[t]));
			if(!o)
				dep[to[t]]=0;
			c[t]-=o,c[t^1]+=o;
			rest-=o;
		}
	now[u]=t;
	return F-rest;
}
inline int dinic(){
	int ans=0;
	while(bfs(s)){
		int delta=0;
		while(delta=dfs(s,inf))
			ans+=delta;
	}
	return ans;
}
inline void add_edge(int u,int v,int w){
	++tot,nxt[tot]=head[u],head[u]=tot,to[tot]=v,c[tot]=w;
}
int main(){
	scanf("%d%d",&m,&n);
	for(int i=1;i<=m;++i)
		scanf("%d",&a[i]);
	s=0,t=n+1;
	for(int i=1,k,x;i<=n;++i){
		scanf("%d",&k);
		while(k--){
			scanf("%d",&x);
			if(!pre[x]){
				add_edge(0,i,a[x]);
				add_edge(i,0,0);
			} else{
				add_edge(pre[x],i,inf);
				add_edge(i,pre[x],0);
			}
			pre[x]=i;
		}
		scanf("%d",&k);
		add_edge(i,t,k);
		add_edge(t,i,0);
	}
	printf("%d\n",dinic());
	return 0;
}
2023/7/13 15:44
加载中...