一看到这个要求的式子就能想到01分数规划,二分答案x后任意两个人对答案的贡献是a[i][j]-xb[i][j]。这样问题就转化为一个二分图匹配,边权就是这个贡献。
显然这是一个裸的费用流,首先建立超级源点和超级汇点,然后所有边的流量上限都是1,男生的n个点和女生的n个点之间两两连边,费用单价就是对答案贡献取反(因为求的是最大费用),加反向边,跑一下费用流就好了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define inf 20000000.0
#define mod 1000000007
#define N 40000
#define M 205
#define eps 1e-7
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
queue<int> q;
int to[N],sz[N],nxt[N],head[M],vis[N],list[N],fa[N];
int tot,S,T,n,i,j;
double cost,l,r,res,mid,ct[N],dis[N];
int a[M][M],b[M][M];
void ADD(int x,int y,int u,double cost)
{to[++tot]=y; sz[tot]=u; ct[tot] = cost; nxt[tot] = head[x]; head[x] = tot;}
void add(int x,int y,double z) {ADD(x,y,1,z); ADD(y,x,0,-z);}
bool spfa()
{
int i,h,t,nt,nw;
fo(i,S,T) dis[i] = inf , vis[i] = 0;
vis[S] = 1; dis[S] = 0;
q.push(S);
while (!q.empty())
{
nw = q.front(); vis[nw] = 0; q.pop();
for (i = head[nw]; i ; i = nxt[i])
{
nt = to[i];
if (sz[i] && (dis[nt] - (dis[nw]+ct[i]) >= eps))
{
dis[nt] = dis[nw] + ct[i];
fa[nt] = i;
if (!vis[nt]) {q.push(nt); vis[nt] = 1;}
}
}
}
if (dis[T] == inf) return false;
int f = inf;
for (i = fa[T]; i ; i = fa[to[i^1]]) f = min(f,sz[i]);
for (i = fa[T]; i ; i = fa[to[i^1]]) sz[i] -= f , sz[i^1] += f;
cost += dis[T] * f;
return true;
}
bool judge(double x)
{
int i,j;
tot = 1; memset(head,0,sizeof(head));
fo(i,1,n)
fo(j,1,n)
add(i,j+n,-(a[i][j] - 1.0 * x * b[i][j]));
fo(i,1,n) add(S,i,0) , add(i+n,T,0);
cost = 0; while (spfa()); cost = -cost;
return cost - 0 >= eps;
}
int main()
{
scanf("%d",&n); S = 0; T = 2 * n + 1;
fo(i,1,n) fo(j,1,n) scanf("%d",&a[i][j]);
fo(i,1,n) fo(j,1,n) scanf("%d",&b[i][j]);
l = 0.0; r = 10000.0; res = 0.0;
while (r - l >= eps)
{
mid = (l + r) / 2;
if (judge(mid)) l = mid , res = mid; else r = mid;
}
printf("%.6lf",res);
return 0;
}