标签:io for amp size ad sp on ef
//来自潘神的优化 #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 0x3fffffff #define N 1100 struct node { int u,v,w,next; }bian[N*N*4]; int head[N],yong,dis[N],work[N]; void init(){ yong=0; memset(head,-1,sizeof(head)); } void addbian(int u,int v,int w) { bian[yong].u=u; bian[yong].v=v; bian[yong].w=w; bian[yong].next=head[u]; head[u]=yong++; } void add(int u,int v,int w) { addbian(u,v,w); addbian(v,u,0); } int min(int a,int b) { return a<b?a:b; } int bfs(int s,int t) { memset(dis,-1,sizeof(dis)); queue<int>q; q.push(s); dis[s]=0; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=bian[i].next) { int v=bian[i].v; if(bian[i].w&&dis[v]==-1) { dis[v]=dis[u]+1; q.push(v); if(v==t) return 1; } } } return 0; } int dfs(int s,int limit,int t) { if(s==t)return limit; for(int &i=work[s];i!=-1;i=bian[i].next) { int v=bian[i].v; if(bian[i].w&&dis[v]==dis[s]+1) { int tt=dfs(v,min(limit,bian[i].w),t); if(tt) { bian[i].w-=tt; bian[i^1].w+=tt; return tt; } } } return 0; } int dinic(int s,int t) { int ans=0; while(bfs(s,t)) { memcpy(work,head,sizeof(head)); while(int tt=dfs(s,inf,t)) ans+=tt; } return ans; } int vis[N],k; int dfs(int u,int pre) { vis[u]=1; for(int &i=work[u];i!=-1;i=bian[i].next) {//优化 int v=bian[i].v; if(bian[i].w&&v!=pre) { if(vis[v]) return 1; if(dfs(v,u)) return 1; } } vis[u]=0; return 0; } int judge(int n) { int i; memset(vis,0,sizeof(vis)); memcpy(work,head,sizeof(head));//优化 for(i=1;i<=n;i++) { if(dfs(i,-1)) return 1; } return 0; } int main() { int m,f,i,j,s,n,t,suma,sumb,T,cou=0; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); k=9; s=0;t=n+m+1; init(); suma=0;sumb=0; for(i=1;i<=n;i++) { scanf("%d",&f); suma+=f; add(s,i,f); } for(i=1;i<=m;i++) { scanf("%d",&f); sumb+=f; add(i+n,t,f); } for(i=1;i<=n;i++) for(j=1;j<=m;j++) add(i,j+n,k); if(suma!=sumb) { printf("Case #%d: So naive!\n",++cou); continue; } f=dinic(s,t); // printf("%d\n",f); if(f!=suma) { printf("Case #%d: So naive!\n",++cou); continue; } if(judge(n)) printf("Case #%d: So young!\n",++cou); else printf("Case #%d: So simple!\n",++cou); } return 0;}
hdu 4975最大流与4888类似但是有很吊的优化最大流,布布扣,bubuko.com
标签:io for amp size ad sp on ef
原文地址:http://blog.csdn.net/u011483306/article/details/38736287