求助卡常!!
查看原帖
求助卡常!!
140876
syzf2222楼主2020/11/24 23:30

显然的网络流。

这常卡得没道理啊!

#include<bits/stdc++.h>
using namespace std;
#define re register
#define inf 1e9
const int maxn=3e6+10;
int n,m,ans; 
inline int read(){
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
	return x*f;
}
int beg[maxn],nex[maxn],to[maxn],w[maxn],e;
inline void add(int x,int y,int z){
	nex[e]=beg[x];beg[x]=e;to[e]=y;w[e]=z;++e;
	nex[e]=beg[y];beg[y]=e;to[e]=x;w[e]=0;++e;
}
int dep[maxn];queue<int>q;
inline int bfs(){
	for(re int i=0;i<=n+m+1;++i)
   	  dep[i]=0;
	dep[0]=1;q.push(0);
	while(!q.empty()){
		int x=q.front();
		q.pop();
		for(re int i=beg[x];~i;i=nex[i])
			if(w[i]&&!dep[to[i]]){
				dep[to[i]]=dep[x]+1;
				q.push(to[i]);
			}
	}
	return dep[n+m+1]!=0;
}
inline int dfs(int x,int lim){
	if(x==n+m+1||!lim)return lim;
	int res=0;
	for(re int i=beg[x];~i;i=nex[i])
		if(w[i]&&dep[to[i]]==dep[x]+1){
			int f=dfs(to[i],min(w[i],lim));
			if(!f){dep[to[i]]=-1;continue;}
			lim-=f;res+=f;
			w[i]-=f;w[i^1]+=f;
		}
	return res;
}
int main(){
	memset(beg,-1,sizeof(beg));
	n=read(),m=read();
	int x,y,t;
	for(re int i=1;i<=n;++i){
		x=read();t=read();
		add(m+i,n+m+1,x);ans+=x;
		for(re int j=1;j<=t;++j){
			x=read();y=read();
			add(x,m+i,y);
		}
	}
	for(re int i=1;i<=m;++i){
		x=read();
		add(0,i,x);
	}
	while(bfs())ans-=dfs(0,inf);
	printf("%d\n",ans);
	return 0;
}
2020/11/24 23:30
加载中...