Luogu

BZOJ

分析

比较套路了。

先上0/1分数规划。二分一个$mid$,$\large\frac{\sum a[i]}{\sum b[i]}>mid\Rightarrow\sum a[i]-mid\times\sum b[i]>0\Rightarrow\sum(a[i]-mid\times b[i])>0$。

然后,按二分图匹配的方式建图,男生$i$连向女生$j$的边的费用设成$a[i][j]-mid*b[i][j]$。

每次二分到一个$mid$时建出图,如果最小费用$\geq0$就可行,否则不行。

代码

//It is made by M_sea
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#define re register
using namespace std;

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int N=100+5;
const int INF=0x3f3f3f3f;
const double EPS=1e-7;

int n,s,t;
int a[N][N],b[N][N];

struct Edge { int v,w,nxt; double c; };
Edge e[20010];
int head[210],cnt=0;

inline void addEdge(int u,int v,int w,double c) {
    e[cnt].v=v,e[cnt].w=w,e[cnt].c=c,e[cnt].nxt=head[u],head[u]=cnt++;
    e[cnt].v=u,e[cnt].w=0,e[cnt].c=-c,e[cnt].nxt=head[v],head[v]=cnt++;
}

double dis[210];
int inq[210],last[210];

inline int SPFA() {
    for (re int i=s;i<=t;++i) dis[i]=-INF,inq[i]=0,last[i]=-1;
    queue<int> Q; Q.push(s); dis[s]=0,inq[s]=1;
    while (!Q.empty()) {
        int u=Q.front(); Q.pop(); inq[u]=0;
        for (re int i=head[u];i!=-1;i=e[i].nxt) {
            int v=e[i].v; double c=e[i].c;
            if (e[i].w&&dis[u]+c>dis[v]) {
                dis[v]=dis[u]+c,last[v]=i;
                if (!inq[v]) inq[v]=1,Q.push(v);
            }
        }
    }
    return last[t]!=-1;
}

inline double MCMF() {
    double ans=0;
    while (SPFA()) {
        int flow=INF;
        for (re int i=last[t];i!=-1;i=last[e[i^1].v])
            flow=min(flow,e[i].w);
        for (re int i=last[t];i!=-1;i=last[e[i^1].v])
            e[i].w-=flow,e[i^1].w+=flow;
        ans+=dis[t]*flow;
    }
    return ans;
}

inline int check(double mid) {
    memset(head,-1,sizeof(head)); cnt=0;
    s=0,t=2*n+1;
    for (re int i=1;i<=n;++i) addEdge(s,i,1,0);
    for (re int i=1;i<=n;++i) addEdge(i+n,t,1,0);
    for (re int i=1;i<=n;++i)
        for (re int j=1;j<=n;++j)
            addEdge(i,j+n,1,a[i][j]-mid*b[i][j]);
    double cost=MCMF();
    return cost>=0;
}

int main() {
    n=read();
    for (re int i=1;i<=n;++i)
        for (re int j=1;j<=n;++j)
            a[i][j]=read();
    for (re int i=1;i<=n;++i)
        for (re int j=1;j<=n;++j)
            b[i][j]=read();
    double L=0,R=1e6;
    while (R-L>=EPS) {
        double mid=(L+R)/2;
        if (check(mid)) L=mid;
        else R=mid;
    }
    printf("%.6f\n",L);
    return 0;
}
最后修改:2019 年 09 月 24 日 10 : 03 PM