JZOJ 1259. 牛棚安排
题目
分析
二分答案加网络流
我边上加多一个忧郁值,如果我忧郁值大于当前枚举就不能流
代码
1 #include<iostream> 2 #include<queue> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 const int N=1e3+10; 7 const int B=21; 8 const int inf=2147483647; 9 int s,t,l; 10 int n,b,ans; 11 int cnt=1,mid; 12 int a[10001]; 13 int dis[10001]; 14 int list[110001]; 15 struct node { 16 int u,v,c,w,nx; 17 }g[2*N*B+2*N+2*B]; 18 void add(int u,int v,int w,int c) { 19 g[++cnt]=(node){u,v,c,w,list[u]};list[u]=cnt; 20 g[++cnt]=(node){v,u,0,0,list[v]};list[v]=cnt; 21 } 22 bool bfs() 23 { 24 queue<int> q; 25 while(!q.empty()) q.pop(); 26 memset(dis,0,sizeof(dis)); 27 q.push(s); 28 dis[s]=1; 29 while (!q.empty()) 30 { 31 int x=q.front(); q.pop(); 32 for (int i=list[x];i;i=g[i].nx) 33 { 34 int y=g[i].v; 35 if (g[i].c&&!dis[y]&&(l<=g[i].w&&g[i].w<=l+mid-1||g[i].w==0)) 36 { 37 dis[y]=dis[x]+1; 38 if (y==t) return 1; 39 q.push(y); 40 } 41 } 42 } 43 return 0; 44 } 45 int dfs(int x,int maxf) 46 { 47 if (x==t) return maxf; 48 int ret=0; 49 for (int i=list[x];i;i=g[i].nx) 50 { 51 int y=g[i].v; 52 if (g[i].c&&dis[y]==dis[x]+1&&(l<=g[i].w&&g[i].w<=l+mid-1||g[i].w==0)) 53 { 54 int f=dfs(y,min(g[i].c,maxf-ret)); 55 if (!f) dis[y]=-1; 56 g[i].c-=f; 57 g[i^1].c+=f; 58 ret+=f; 59 if (ret==maxf) return ret; 60 } 61 } 62 return ret; 63 } 64 bool dinic() 65 { 66 int flow; 67 for (l=1;l<=max(1,b-mid+1);l++) 68 { 69 flow=0; 70 while (bfs()) 71 flow+=dfs(s,inf); 72 for (int i=2;i<=cnt;i+=2) 73 if (g[i].u==s) g[i].c=a[g[i].v],g[i^1].c=0; 74 else g[i].c=1,g[i^1].c=0; 75 if (flow==n) return 1; 76 } 77 return 0; 78 } 79 int main () 80 { 81 cin>>n>>b; 82 s=0,t=n+b+1; 83 for (int i=1;i<=n;i++) 84 for (int j=1,c;j<=b;j++) 85 { 86 cin>>c; 87 add(c,i+b,j,1); 88 } 89 for (int i=1;i<=b;i++) scanf("%d",&a[i]),add(s,i,0,a[i]); 90 for (int i=1;i<=n;i++) add(i+b,t,0,1); 91 int l=1,r=20; 92 while (l<=r) 93 { 94 mid=l+r>>1; 95 if (dinic()) ans=mid,r=mid-1; 96 else l=mid+1; 97 } 98 cout<<ans; 99 }
为何要逼自己长大,去闯不该闯的荒唐