BZOJ 1391 网络流
vis[0]没有清零查一年…
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 55555
#define M 3666666
int n,m,xx,yy,first[N],vis[N],nxt[M],v[M],w[M],tot,all,jy,ed,ans,cur[N],q[M],head,tail;
void Add(int x,int y,int z){}
void add(int x,int y,int z){
w[tot]=z,v[tot]=y,nxt[tot]=first[x],first[x]=tot++;
w[tot]=0,v[tot]=x,nxt[tot]=first[y],first[y]=tot++;
}
bool tell(){
for(int i=1;i<=ed;i++)vis[i]=-1;
head=tail=0;
while(head<=tail){
int t=q[head++];
for(int i=first[t];~i;i=nxt[i])
if(!~vis[v[i]]&&w[i])
vis[v[i]]=vis[t]+1,q[++tail]=v[i];
}
return ~vis[ed];
}
int zeng(int x,int y){
if(x==ed)return y;
int r=0;
for(int i=cur[x];~i&&y>r;i=nxt[i])
if(vis[v[i]]==vis[x]+1&&w[i]){
int t=zeng(v[i],min(w[i],y-r));
w[i]-=t,w[i^1]+=t,r+=t;
if(w[i]>0)cur[x]=i;
}
if(!r)vis[x]=-1;
return r;
}
inline int read(){
int x=0;char p=getchar();
while(p<'0'||p>'9')p=getchar();
while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();
return x;
}
int main(){
memset(first,-1,sizeof(first));
scanf("%d%d",&n,&m),ed=n+m+1;
for(int i=1;i<=n;i++){
xx=read(),jy=read();
all+=xx,add(0,i,xx);
while(jy--)xx=read(),yy=read(),add(i,xx+n,yy);
}
for(int i=1;i<=m;i++)xx=read(),add(i+n,ed,xx);
while(tell()){
for(int i=0;i<=ed;i++)cur[i]=first[i];
while(jy=zeng(0,0x3fffffff))ans+=jy;
}
printf("%d\n",all-ans);
}