BZOJ_4819_[Sdoi2017] Freshman Ball_01 Score Planning + Expense Flow

BZOJ_4819_[Sdoi2017] Freshman Ball_01 Score Planning + Expense Flow

Description

The school organized a freshman dance. As an experienced senior, Cathy was responsible for arranging dance partners for the students. There are n boys and n girls at the dance
Buy a boy and a girl to dance with each other as partners. Cathy collected the relationship between these classmates, such as two people who knew each other before and could not calculate 
a[i][j] , which represents the joy of the i-th boy and the j-th girl when they dance together. Cathy also needs to consider whether it is convenient for two people to dance together,
For example, will the difference in height and weight be too large, and b[i][j] can be calculated to indicate the degree of incompatibility when the i-th boy and the j-th girl dance together. Of course,
There are many other issues to consider. Cathy wants to use a program to find a solution through a[i][j] and b[i][j], and then manually fine-tune the solution. C
athy finds you and wants you to help her write that program. There are n pairs of dance partners in a scheme, assuming that the joy levels of no dance partners are a'1, a'2, ..., a'n, respectively,
Suppose the degree of incompatibility of each pair of dance partners is b'1, b'2, ..., b'n, respectively. make
C=(a'1+a'2+...+a'n)/(b'1+b'2+...+b'n), Cathy wants the maximum value of C.

Input

The first line contains an integer n.
Next n lines, each line contains n integers, and the jth number on the i-th line represents a[i][j].
Next n lines, each line contains n integers, and the jth number in the i-th line represents b[i][j].
1<=n<=100,1<=a[i][j],b[i][j]<=10^4

Output

One number per line, representing the maximum value of C. Round off to 6 decimal places, the decimal output of the player needs to be equal to the standard output

Sample Input

3
19 17 16
25 24 23
35 36 31
9 5 6
3 4 2
7 8 9

Sample Output

5.357143

Dichotomous answer x, the new weight is ab*x.
The question means to find the maximum weighted matching of the bipartite graph, and convert it into the maximum cost and maximum flow.
 
Code:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef double f2;
#define N 250
#define M 500050
#define S (n+n+1)
#define T (n+n+2)
int head[N],to[M],nxt[M],flow[M],cnt=1,n,a[105][105],b[105][105],Q[N],l,r;
int inq[N],path[N];
f2 val[M],dis[N];
inline void add (int u, int v, int f, f2 va) {
    to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; flow[cnt]=f; val[cnt]=va;   
    to[++cnt]=u; nxt[cnt]=head[v]; head[v]=cnt; flow[cnt]=0; val[cnt]=-va;
}
bool spfa() {
    memset(dis,0xc2,sizeof(dis));
    memset(path,0,sizeof(path));
    dis[S]=0;inq[S]=1;l=r=0;Q[r++]=S;
    while(l!=r) {
        int x=Q[l++],i;if(l==S) l=0; inq[x]=0;
        for(i=head[x];i;i=nxt[i]) {
            if(dis[to[i]]<dis[x]+val[i]&&flow[i]) {
                dis[to[i]]=dis[x]+val[i];
                path[to[i]]=i^1;
                if(!inq[to[i]]) {
                    inq[to[i]]=1; Q[r++]=to[i]; if(r==S) r=0;
                }
            }
        }
    }
    return path[T];
}
bool check(f2 x) {
    int i,j;
    memset(head,0,sizeof(head)); cnt=1;
 
    for(i=1;i<=n;i++) {
        add(S,i,1,0.0);
        add(i+n,T,1,0.0);
    }
    for(i=1;i<=n;i++) {
        for(j=1;j<=n;j++) {
            add(i,n+j,1,a[i][j]-x*b[i][j]);
        }
    }
    f2 maxc=0;
    while(spfa()) {
        int i,nf=1<<30;
        for(i=T;i!=S;i=to[path[i]]) {
            nf=min(nf,flow[path[i]^1]);
        }
        for(i=T;i!=S;i=to[path[i]]) {
            flow[path[i]]+=nf;
            flow[path[i]^1]-=nf;
            maxc+=nf*val[path[i]^1];
        }
    }
    return maxc>=0;
}
int main() {
    scanf("%d",&n);
    int i,j;
    for(i=1;i<=n;i++) {
        for(j=1;j<=n;j++) {
            scanf("%d",&a[i][j]);
        }
    }
    for(i=1;i<=n;i++) {
        for(j=1;j<=n;j++) {
            scanf("%d",&b[i][j]);
        }
    }
    f2 ll=0,rr=1000000;
    for(i=1;i<=60;i++) {
        f2 mid=(ll+rr)/2;
        if(check(mid)) ll=mid;
        else rr=mid;
    }
    printf("%.6lf\n",ll);
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325473139&siteId=291194637