AC日记——pigs poj 1149
思路:
最大流;
代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 105 #define maxm 1005 #define maxque 200005 #define INF 0x7fffffff int n,m,val[maxm],last[maxm],head[maxn],s,t,deep[maxn]; int E[maxque],V[maxque],F[maxque],cnt=1,ans,que[maxque]; inline void in(int &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0') Cget=getchar(); while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } } inline void edge_add(int u,int v,int f) { E[++cnt]=head[u],V[cnt]=v,F[cnt]=f,head[u]=cnt; E[++cnt]=head[v],V[cnt]=u,F[cnt]=0,head[v]=cnt; } inline int min(const int &tops_,const int &tops__) { if(tops_<tops__) return tops_; else return tops__; } inline bool bfs() { for(int i=s;i<=t;i++) deep[i]=-1; int h=0,tail=1,now;que[h]=s,deep[s]=0; for(;h<tail;) { now=que[h++]; for(int i=head[now];i;i=E[i]) { if(deep[V[i]]<0&&F[i]) { deep[V[i]]=deep[now]+1; if(V[i]==t) return true; que[tail++]=V[i]; } } } return false; } inline int flowing(int now,int flow) { if(now==t||flow<=0) return flow; int oldflow=0,pos; for(int i=head[now];i;i=E[i]) { if(deep[V[i]]==deep[now]+1&&F[i]) { pos=flowing(V[i],min(flow,F[i])); flow-=pos,oldflow+=pos,F[i]-=pos,F[i^1]+=pos; if(!flow) return oldflow; } } if(!oldflow) deep[now]=-1; return oldflow; } int main() { in(m),in(n),t=n+1; for(int i=1;i<=m;i++) in(val[i]); for(int i=1;i<=n;i++) { int pos=0,now=0,num=0;in(num); for(int j=1;j<=num;j++) { in(pos); if(!last[pos]) last[pos]=i,now+=val[pos]; else edge_add(last[pos],i,INF),last[pos]=i; } in(pos),edge_add(i,t,pos); if(now) edge_add(s,i,now); } while(bfs()) ans+=flowing(s,INF); printf("%d\n",ans); return 0; }