Bzoj1305 [cqoi2009]dance跳舞

Posted by yjjr's blog on February 6, 2018

题目

Description

YYY

YYY

YYY

Sample Output 3 HINT

N<=50 K<=30

Source

x域表示喜欢，y域表示不喜欢

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define inf 0x7fffffff
using namespace std;
{
ll f=1,x=0;char ch=getchar();
while(ch>'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e3+6;
int last[maxn],que[maxn],h[maxn],Map[maxn][maxn];
int cnt=1,ans,Max,mid,n,k;
char ch[1006];
struct edge{int to,next,v;}e[1000006];
void ins(int u,int v,int w){e[++cnt]=(edge){v,last[u],w};last[u]=cnt;}
void insert(int u,int v,int w){ins(u,v,w);ins(v,u,0);}
bool bfs()
{
mem(h,-1);que[0]=0;h[0]=0;
reg(now)
if(e[i].v&&h[e[i].to]==-1){h[e[i].to]=h[now]+1;que[tail++]=e[i].to;}
}
if(h[maxn-1]==-1)return 0;else return 1;
}

int dfs(int x,int f)
{
if(x==maxn-1)return f;
int w,used=0;
reg(x)
if(e[i].v&&h[e[i].to]==h[x]+1){
w=f-used;w=dfs(e[i].to,min(w,e[i].v));
e[i].v-=w;e[i^1].v+=w;
used+=w;if(used==f)return f;
}
if(!used)h[x]=-1;
return used;
}

void dinic(){while(bfs())ans+=dfs(0,inf);}
void build()
{
cnt=1;mem(last,0);
rep(i,1,n)insert(0,i,mid);//超级源点到男生的连接
rep(i,1,n)insert(i,i+500,k);//男生x和y之间互相连接
rep(i,1,n)insert(n+i+500,n+i,k);//女生y和x之间互相连接
rep(i,1,n)insert(n+i,maxn-1,mid);//女生到超级汇点的连接
rep(i,1,n)
rep(j,1,n)
if(Map[i][j])insert(i,n+j,1);
else insert(i+500,n+j+500,1);
}
int main()
{
cin>>n>>k;
rep(i,1,n){
cin>>ch;
rep(j,1,n)
if(ch[j-1]=='Y')Map[i][j]=1;
}
int l=0,r=50;
while(l<=r){
mid=(l+r)/2;
build();
ans=0;dinic();
if(ans>=n*mid){Max=mid;l=mid+1;}
else r=mid-1;
}
cout<<Max<<endl;
return 0;
}