Article From:https://www.cnblogs.com/zhoushuyu/p/9060714.html

[BZOJ4819][SDOI2017] new dance”

bzoj
luogu

meaning”

Yes\(n\)A boy and a boy\(n\)A girl. There are 22 partners dancing between them.
Known\(i\)A boy and a child\(j\)There are two parameters for a girl to dance with\(a_{i,j}\)and\(b_{i,j}\)
Now requires an arrangement to make\(a_{i,j}\)The sum divided by\(b_{i,j}\)The sum of the quotient is as large as possible.
In formalization, it is to find a length.\(n\)Arrangement\(\{p_i\}\),Maximization\(L=\frac{\sum_{i=1}^{n}a_{i,p_i}}{\sum_{i=1}^{n}b_{i,p_i}}\)

sol

Fractional planning.
Two points one answer\(mid\),Set the weights for all the matched edges to be\(a_{i,j}-mid*b_{i,j}\)
Next is to ask whether there is a weight and greater than.\(0\)The perfect match.
The cost flow is straight up.It’s so slow to run, QAQ

code

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int gi()
{
    int x=0,w=1;char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-') w=0,ch=getchar();
    while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return w?x:-x;
}
const int N = 105;
const double eps = 1e-8;
struct edge{int to,nxt,w;double cost;}G[N*N<<2];
int n,a[N][N],b[N][N],head[N<<1],cnt,S,T,vis[N<<1],pe[N<<1];
double dis[N<<1],ans;queue<int>Q;
void link(int u,int v,int w,double cost){
    G[++cnt]=(edge){v,head[u],w,cost};head[u]=cnt;
    G[++cnt]=(edge){u,head[v],0,-cost};head[v]=cnt;
}
bool spfa(){
    memset(dis,0xfe,sizeof(dis));
    dis[S]=0;Q.push(S);
    while (!Q.empty()){
        int u=Q.front();Q.pop();
        for (int e=head[u];e;e=G[e].nxt){
            int v=G[e].to;
            if (G[e].w&&dis[v]<dis[u]+G[e].cost){
                dis[v]=dis[u]+G[e].cost;pe[v]=e;
                if (!vis[v]) vis[v]=1,Q.push(v);
            }
        }
        vis[u]=0;
    }
    if (dis[T]==dis[0]) return false;
    ans+=dis[T];
    for (int i=T;i!=S;i=G[pe[i]^1].to)
        --G[pe[i]].w,++G[pe[i]^1].w;
    return true;
}
bool check(double k){
    memset(head,0,sizeof(head));cnt=1;
    for (int i=1;i<=n;++i) link(S,i,1,0),link(i+n,T,1,0);
    for (int i=1;i<=n;++i)
        for (int j=1;j<=n;++j)
            link(i,j+n,1,(double)a[i][j]-k*b[i][j]);
    ans=0;
    while (spfa()) ;
    return ans>-eps;
}
int main(){
    n=gi();S=2*n+1;T=S+1;
    for (int i=1;i<=n;++i)
        for (int j=1;j<=n;++j)
            a[i][j]=gi();
    for (int i=1;i<=n;++i)
        for (int j=1;j<=n;++j)
            b[i][j]=gi();
    double l=0,r=10000;
    while (r-l>eps){
        double mid=(l+r)/2;
        if (check(mid)) l=mid;
        else r=mid;
    }
    printf("%.6lf\n",l);
    return 0;
}

Similar Posts:

Link of this Article: [BZOJ4819][SDOI2017] new dance

Leave a Reply

Your email address will not be published. Required fields are marked *