Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3611 | Accepted: 1245 |
Description
Input
Output
Sample Input
6 4 1 2 3 4 2 3 1 4 4 2 3 1 3 1 2 4 1 3 4 2 1 4 2 3 2 1 3 2
Sample Output
2
Hint
#include <iostream> #include<cstdio> #include<string> using namespace std; #define inf 1<<30 #define maxM 100005 #define maxN 1050 #define cc(m,v) memset(m,v,sizeof(m)) struct node{ int u,v,f,next; }edge[maxM]; int head[maxN],p,lev[maxN],cur[maxN]; int que[maxM],maze[1005][30],cap[30]; void ainit(){ p=0,cc(head,-1); } bool bfs(int s,int t){ int u,i,v,qin=0,qout=0; cc(lev,0),lev[s]=1,que[qin++]=s; while(qout!=qin){ u=que[qout++]; for(i=head[u];i!=-1;i=edge[i].next) if(edge[i].f>0 && lev[v=edge[i].v]==0){ lev[v]=lev[u]+1,que[qin++]=v; if(v==t) return 1; } } return lev[t]; } int dinic(int s,int t){ int i,k,u,f; int flow=0,qin; while(bfs(s,t)){ memcpy(cur,head,sizeof(head)); u=s,qin=0; while(1){ if(u==t){ for(k=0,f=inf;k<qin;k++) if(edge[que[k]].f<f) f=edge[que[i=k]].f; for(k=0;k<qin;k++) edge[que[k]].f-=f,edge[que[k]^1].f+=f; flow+=f,u=edge[que[qin=i]].u; } for(i=cur[u];cur[u]!=-1;i=cur[u]=edge[cur[u]].next) if(edge[i].f>0 && lev[u]+1==lev[edge[i].v]) break; if(cur[u]!=-1) que[qin++]=cur[u],u=edge[cur[u]].v; else{ if(qin==0) break; lev[u]=-1,u=edge[que[--qin]].u; } } } return flow; } void addedge(int u,int v,int f){ edge[p].u=u,edge[p].v=v,edge[p].f=f,edge[p].next=head[u],head[u]=p++; edge[p].u=v,edge[p].v=u,edge[p].f=0,edge[p].next=head[v],head[v]=p++; } void cal(int n,int m){ int i,j,u,v; for(i=1;i<=m;i++) for(j=1;j+i-1<=m;j++){ ainit(); for(u=1;u<=n;u++) addedge(0,u,1); for(u=1;u<=m;u++) addedge(u+n,n+m+1,cap[u]); for(u=1;u<=n;u++) for(v=j;v<j+i;v++) addedge(u,maze[u][v]+n,1); if(dinic(0,n+m+1)==n){ printf("%d\n",i); return ; } } } int main(){ int n,m,i,j; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&maze[i][j]); for(i=1;i<=m;i++) scanf("%d",&cap[i]); cal(n,m); return 0; }