1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#define ts cout<<"ok"<<endl #define ll long long #define hh puts("") using namespace std; int n,b,g[1005][25],ans=1e9,st,ed,tot,can[25],head[1025],cnt,d[1025],cur[1025]; struct { int v,nx,s; }e[100005]; inline int read(){ int ret=0,ff=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') ff=-ff;ch=getchar();} while(isdigit(ch)){ret=(ret<<3)+(ret<<1)+ch-'0';ch=getchar();} return ret*ff; } void write(int x){ if(x>9) write(x/10); putchar(x%10+48); } inline void add(int x,int y,int s){ e[++cnt].v=y; e[cnt].s=s; e[cnt].nx=head[x]; head[x]=cnt; } inline bool bfs(){ for(int i=st;i<=ed;i++) cur[i]=head[i],d[i]=0; d[st]=1; queue<int> q; q.push(st); while(!q.empty()){ int now=q.front(); q.pop(); for(int i=head[now];i;i=e[i].nx){ int v=e[i].v; if(!d[v]&&e[i].s){ d[v]=d[now]+1; q.push(v); } } } return d[ed]; } int dfs(int now,int ma){ if(now==ed){ tot+=ma; return ma; } int used=0,t; for(int i=cur[now];i;i=e[i].nx){ cur[now]=i; int v=e[i].v; if(d[v]==d[now]+1&&e[i].s){ if(t=dfs(v,min(e[i].s,ma-used))){ e[i].s-=t; e[i^1].s+=t; used+=t; if(used==ma) break; } } } return used; } void build(int l,int r){ cnt=1; memset(head,0,sizeof(head)); for(int i=1;i<=n;i++) add(st,i,1),add(i,st,0); for(int i=1;i<=n;i++) for(int j=l;j<=r;j++) add(i,n+g[i][j],1),add(n+g[i][j],i,0); for(int i=1;i<=b;i++) add(n+i,ed,can[i]),add(ed,n+i,0); } signed main(){ n=read(),b=read(); st=0,ed=n+b+1; for(int i=1;i<=n;i++) for(int j=1;j<=b;j++) g[i][j]=read(); for(int i=1;i<=b;i++) can[i]=read(); for(int i=1;i<=b;i++) for(int j=i;j<=b;j++){ if(j-i+1>=ans) continue; build(i,j); tot=0; while(bfs()) dfs(st,1e9); if(tot==n) ans=j-i+1; } printf("%d",ans); return 0; }
|
近期评论