官方题解是一个n2logn的dp做法
不过有一个简单易想的费用流做法
对每个小精灵,连边(A,i,1,pi) (B,i,1,ui) (i,t,1,0) (i,t,1,-pi*ui)
最后连边(s,A,a,0) (s,B,b,0)
跑最大费用最大流即可,注意精度误差
#include<bits/stdc++.h> using namespace std; struct way{int po,next,flow;double cost;} e[];
const double eps=1e-;
int pre[],p[],cur[],q[];
double d[],c[];
bool v[];
int len,n,m,a,b,t; void add(int x,int y,int f,double c)
{
e[++len].po=y;
e[len].flow=f;
e[len].cost=c;
e[len].next=p[x];
p[x]=len;
} void build(int x,int y,int f,double c)
{
add(x,y,f,c);
add(y,x,,-c);
} bool spfa()
{
for (int i=; i<=t; i++) d[i]=-1e20;
memset(v,,sizeof(v));
d[]=;
int f=,r=;q[]=;
while (f<=r)
{
int x=q[f++];
v[x]=;
for (int i=p[x]; i!=-; i=e[i].next)
{
int y=e[i].po;
if (e[i].flow&&d[x]+e[i].cost-eps>d[y])
{
d[y]=d[x]+e[i].cost;
pre[y]=x; cur[y]=i;
if (!v[y])
{
q[++r]=y;
v[y]=;
}
}
}
}
return d[t]>-1e20;
} double cost()
{
double s=;
while (spfa())
{
s+=d[t];
for (int i=t; i; i=pre[i])
{
int j=cur[i];
e[j].flow--;
e[j^].flow++;
}
}
return s;
} int main()
{
len=-;
memset(p,,sizeof(p));
scanf("%d%d%d",&n,&a,&b);
t=n+;
for (int i=; i<=n; i++)
{
scanf("%lf",&c[i]);
build(n+,i,,c[i]);
build(i,t,,);
}
for (int i=; i<=n; i++)
{
double x;
scanf("%lf",&x);
build(n+,i,,x);
build(i,t,,-c[i]*x);
}
build(,n+,a,);
build(,n+,b,);
printf("%.5lf\n",cost());
}