这题的技巧就是:枚举---旋转卡壳法。
题目大意:
每头牛对每个谷仓有一个喜欢程度,FJ的目的就是要使得每头牛的happy值尽可能的相同,求最小的范围。
这题的枚举还是很有技巧的。虽然知道怎么来滑动窗口。。 但是我的网络流EK算法太不给力了!
没办法.. 去学习了下SAP勉强把这题切掉了= =
#include<iostream> #include<string> #include<cstdio> #include<algorithm> #define MN 1111 #define INF 0x0FFFFFFF #define CC(m,what) memset(m,what,sizeof(m)) #define FOR(i,a,b) for( int i = (a) ; i < (b) ; i ++ ) #define FF(i,a) for( int i = 0 ; i < (a) ; i ++ ) #define FFD(i,a) for( int i = (a)-1 ; i >= 0 ; i --) #define SS(a) scanf("%d",&a) #define LL(a) ((a)<<1) #define RR(a) (((a)<<1)+1) #define SZ(a) ((int)a.size()) #define PP(n,m,a) puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");} #define read freopen("in.txt","r",stdin) #define write freopen("out.txt","w",stdout) #define two(x) ((LL)1<<(x)) #define include(a,b) (((a)&(b))==(b)) template<class T> inline T countbit(T n) {return n?1+countbit(n&(n-1)):0;} template<class T> inline T sqr(T a) {return a*a;} template<class T> inline void checkmin(T &a,T b) {if(a == -1 || a > b)a = b;} template<class T> inline void checkmax(T &a,T b) {if(a < b) a = b;} using namespace std; struct EDGE { int u,v,len; }edge[1000*21]; int map[MN][MN],vis[MN],barn[MN],pre[MN]; int N,B,s,t,ans; bool cmp( EDGE a,EDGE b ){ return a.len<b.len; } void setG() { s=0;t=B+N+1; int i,j,num,at; for( i=1;i<=N;i++ ) for( j=1;j<=B;j++ ) { scanf( "%d",&num ); at=(i-1)*B+j-1; edge[at].u=num; edge[at].v=B+i; edge[at].len=j; } sort( edge,edge+B*N,cmp ); for( i=1;i<=B;i++ ) scanf( "%d",&barn[i] ); } void initG(int l,int r) { int i,j; for( i=0;i<=t;i++ ) for( j=0;j<=t;j++ ) map[i][j]=0; for( i=(l-1)*N;i<r*N;i++ ) map[edge[i].u][edge[i].v]++; for( i=1;i<=B;i++ ) map[s][i]=barn[i]; for( i=B+1;i<=B+N;i++ ) map[i][t]++; } int sap() { int cur[MN],dis[MN],gap[MN]; CC(cur,0);CC(dis,0);CC(gap,0); int u=pre[s]=s,maxflow=0,aug=-1; gap[0]=t+1; while( dis[s]<=t ){ loop: for( int v=cur[u];v<=t;v++ ) if( map[u][v]&&dis[u]==dis[v]+1 ) { cur[u]=v; checkmin(aug,map[u][v]); pre[v]=u; u=v; if( v==t ) { maxflow+=aug; for( u=pre[u];v!=s;v=u,u=pre[u] ) { map[u][v]-=aug; map[v][u]+=aug; } aug=-1; } goto loop; } int mind=t; for( int v=0;v<=t;v++ ) if( map[u][v]&&mind>dis[v] ) { cur[u]=v; mind=dis[v]; } if( (--gap[dis[u]])==0 ) break; gap[dis[u]=mind+1]++; u=pre[u]; } return maxflow; } bool work() { int i,j; if( sap()==N ) return true; else return false; } int main() { while( scanf("%d%d",&N,&B)!=EOF ) { setG(); int range=INF; int r=1,l=1; while( l<=B&&l<=r&&r<=B ) { initG(l,r); if( work() ) { range=min( range,r-l+1 ); l++; } else r++; } printf( "%d\n",range ); } return 0; }