标签:i++ names min 一个 scanf com string 完成 size
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #define inf 1000000000 6 using namespace std; 7 int ans,tot,n,m,T,cnt=1,Q[30005],head[30005],state[30005],cur[30005]; 8 struct edge{ int to,from,v; }e[3000005]; 9 void insert(int x,int y,int v) 10 { 11 e[++cnt].to=y,e[cnt].from=head[x],head[x]=cnt,e[cnt].v=v; 12 e[++cnt].to=x,e[cnt].from=head[y],head[y]=cnt,e[cnt].v=0; 13 } 14 bool bfs() 15 { 16 int p=0,q=1; 17 memset(state,-1,sizeof(state)); 18 state[0]=Q[0]=0; 19 while (p!=q) 20 { 21 int u=Q[p]; p++; if (p==2501) p=0; 22 for (int i=head[u];i;i=e[i].from) if (e[i].v&&state[e[i].to]==-1) state[e[i].to]=state[u]+1,Q[q++]=e[i].to,q=(q==2501)?0:q; 23 } 24 return state[T]!=-1; 25 } 26 int dfs(int x,int maxf) 27 { 28 if (x==T) return maxf; 29 int f=0,r; 30 for (int i=cur[x];i;i=e[i].from) 31 if (e[i].v&&state[e[i].to]==state[x]+1) 32 { 33 r=dfs(e[i].to,min(e[i].v,maxf-f)),e[i].v-=r,e[i^1].v+=r,f+=r; 34 if (e[i].v>0) cur[x]=i; 35 if (f==maxf) return f; 36 } 37 if (!f) state[x]=-1; 38 return f; 39 } 40 void dinic() { while (bfs()) for (int i=0;i<=T;i++) cur[i]=head[i],ans-=dfs(0,inf); } 41 int main() 42 { 43 scanf("%d%d",&n,&m),T=n+m+1; 44 for (int i=1,x,y;i<=n;i++) 45 { 46 scanf("%d%d",&x,&y),insert(0,i,x),ans+=x; 47 for (int j=1,l,r;j<=y;j++) scanf("%d%d",&l,&r),insert(i,n+l,r); 48 } 49 for (int i=1,x;i<=m;i++) scanf("%d",&x),insert(n+i,T,x); 50 dinic(); 51 printf("%d",ans); 52 }
标签:i++ names min 一个 scanf com string 完成 size
原文地址:https://www.cnblogs.com/Comfortable/p/11157384.html