bzoj3993 [SDOI2015]星际战争
二分+网络流。
二分(ans),然后对每个二分的mid:
- 连接S->每个激光武器i,权值为mid*B[i]
- 连接每个机器人j->T,权值为A[j]
- 连接激光武器i->机器人j,前提是i能打j,权值为inf
然后跑一遍Dinic,如果(maxflow>=sum A[i])
说明可以打死 r=mid
否则打不死l=mid+1
注意要精确的(10^{-3})
二分的时候给l,r乘一个(10^3)
midB[i]不变
A[j]改成A[j]1e3就好了
code
// It is made by XZZ
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(a,b,c) for(rg ll a=b;a<=c;a++)
#define drep(a,b,c) for(rg ll a=b;a>=c;a--)
#define erep(a,b) for(rg ll a=fir[b];a;a=nxt[a])
#define il inline
#define rg register
#define vd void
#define inf 1e15
typedef long long ll;
il ll gi(){
rg ll x=0;rg bool flg=0;rg char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')flg=1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return flg?-x:x;
}
ll A[51],B[51];
bool able[51][51];
const ll S=0,T=101,maxn=102,maxm=(50*50+50+50+6)<<1;
ll fir[maxn],nxt[maxm],dis[maxm],id,w[maxm];
il vd _add(ll a,ll b,ll c){nxt[++id]=fir[a],fir[a]=id,dis[id]=b,w[id]=c;}
il vd add(ll a,ll b,ll c){_add(a,b,c),_add(b,a,0);}
ll dep[maxn];
il bool BFS(){
static ll que[maxn],hd,tl,x;static bool vis[maxn];
hd=tl=0;memset(vis,0,sizeof vis);
que[tl++]=S,dep[S]=1,vis[S]=1;
while(hd^tl){
x=que[hd];
erep(i,x)if(!vis[dis[i]]&&w[i])dep[dis[i]]=dep[x]+1,vis[dis[i]]=1,que[tl++]=dis[i];
++hd;
}
return vis[T];
}
il ll Dinic(ll x,ll maxflow){
if(x==T)return maxflow;
ll D,flow=0;
erep(i,x)if(dep[dis[i]]==dep[x]+1&&w[i]){
D=Dinic(dis[i],min(w[i],maxflow-flow));
w[i]-=D,w[i^1]+=D,flow+=D;
if(flow==maxflow)return flow;
}return flow;
}
il ll Dinic(){
ll ret=0;
while(BFS())ret+=Dinic(S,inf);
return ret;
}
int main(){
ll n=gi(),m=gi(),Sigma=0;
rep(i,1,n)A[i]=gi(),Sigma+=A[i];
rep(i,1,m)B[i]=gi();
rep(i,1,m)rep(j,1,n)able[i][j]=gi();
ll mid,l=0,r=1e9;
while(l<r){
mid=(l+r)>>1;
id=1,memset(fir,0,sizeof fir);
rep(i,1,m)add(S,i,mid*B[i]);
rep(i,1,n)add(i+m,T,A[i]*1e3);
rep(i,1,m)rep(j,1,n)if(able[i][j])add(i,j+m,inf);
if(Dinic()>=Sigma*1e3)r=mid;
else l=mid+1;
}printf("%.3f
",l/1e3);
return 0;
}