题解,ozy允许转载。
ozy的题解(我们一起想的)
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
const double eqs=0.0001;
const double inf=999999999;
int n,m;
double A[55],B[55];
struct node{
int x,y,next,other;
double c;
}a[20010];int len=0,last[110];
int h[110],st,ed,s[110];
int map[55][55];
void ins(int x,int y,double c)
{
int k1=++len;
a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=last[x];last[x]=len;
int k2=++len;
a[len].x=y;a[len].y=x;a[len].c=0.0;
a[len].next=last[y];last[y]=len;
a[k1].other=k2;a[k2].other=k1;
}
bool bt_h()
{
memset(h,0,sizeof(h));
int l=1,r=2;s[l]=st;h[st]=1;
while(l!=r)
{
int x=s[l];
for(int i=last[x];i;i=a[i].next)
{
int y=a[i].y;
if(h[y]==0&&a[i].c>eqs) h[y]=h[x]+1,s[r++]=y;
}
l++;
}
return h[ed]!=0;
}
double findflow(int x,double f)
{
if(x==ed) return f;
double t,ans=0;
for(int i=last[x];i;i=a[i].next)
{
int y=a[i].y;
if(ans==f) return ans;
if(h[x]+1==h[y]&&a[i].c>eqs&&ans<f)
{
ans+=(t=findflow(y,min(a[i].c,f-ans)));
a[i].c-=t;a[a[i].other].c+=t;
}
}
if(ans<=eqs) h[x]=0;
return ans;
}
bool check(double x)
{
memset(last,0,sizeof(last));len=0;
st=0;ed=n+m+1;
double tot=0;
for(int i=1;i<=n;i++)
{
tot+=A[i];ins(st,i,A[i]);
for(int j=1;j<=m;j++)
if(map[j][i]) ins(i,j+n,inf);
}
for(int i=1;i<=m;i++) ins(i+n,ed,B[i]*x);
double ans=0;
while(bt_h()) ans+=findflow(st,inf);
return fabs(ans-tot)<=eqs;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lf",&A[i]);
for(int i=1;i<=m;i++) scanf("%lf",&B[i]);
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
scanf("%d",&map[i][j]);
double l=0,r=500000,ans;
while(fabs(l-r)>eqs)
{
double mid=(l+r)/2;
if(check(mid)) ans=mid,r=mid;
else l=mid;
}
printf("%.6lf",ans);
}