4819: [Sdoi2017]新生舞会

4819: [Sdoi2017]新生舞会

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 352 Solved: 177
[Submit][Status][Discuss]
Description

学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴。有n个男生和n个女生参加舞会
买一个男生和一个女生一起跳舞,互为舞伴。Cathy收集了这些同学之间的关系,比如两个人之前认识没计算得出
a[i][j] ,表示第i个男生和第j个女生一起跳舞时他们的喜悦程度。Cathy还需要考虑两个人一起跳舞是否方便,
比如身高体重差别会不会太大,计算得出 b[i][j],表示第i个男生和第j个女生一起跳舞时的不协调程度。当然,
还需要考虑很多其他问题。Cathy想先用一个程序通过a[i][j]和b[i][j]求出一种方案,再手动对方案进行微调。C
athy找到你,希望你帮她写那个程序。一个方案中有n对舞伴,假设没对舞伴的喜悦程度分别是a’1,a’2,…,a’n,
假设每对舞伴的不协调程度分别是b’1,b’2,…,b’n。令
C=(a’1+a’2+…+a’n)/(b’1+b’2+…+b’n),Cathy希望C值最大。
Input

第一行一个整数n。
接下来n行,每行n个整数,第i行第j个数表示a[i][j]。
接下来n行,每行n个整数,第i行第j个数表示b[i][j]。
1<=n<=100,1<=a[i][j],b[i][j]<=10^4
Output

一行一个数,表示C的最大值。四舍五入保留6位小数,选手输出的小数需要与标准输出相等
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
HINT

Source

鸣谢infinityedge上传

[Submit][Status][Discuss]

二分一个答案然后用分数规划的思想跑跑费用流就行了。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<stack>
#define min(a,b) ((a) < (b) ? (a) : (b))
using namespace std;

const int INF = ~0U>>1;
const int maxn = 205;
const int maxm = 2E5 + 20;
typedef double DB;
const DB G = 1E12;
const DB EPS = 1E-9;

struct E{
    int to,cap,flow; DB cost; E(){}
    E(int to,int cap,int flow,DB cost): to(to),cap(cap),flow(flow),cost(cost){}
}edgs[maxm];

int n,s,t,cnt,from[maxn],a[maxn];
DB A[maxn][maxn],B[maxn][maxn],Cost[maxn];
bool inq[maxn];

queue <int> Q;
vector <int> v[maxn];

inline void Add(int x,int y,int cap,DB Cost)
{
    v[x].push_back(cnt); edgs[cnt++] = E(y,cap,0,Cost);
    v[y].push_back(cnt); edgs[cnt++] = E(x,0,0,-Cost);
}

inline bool SPFA()
{
    for (int i = s; i <= t; i++) Cost[i] = -G;
    Q.push(s); a[s] = a[t] = INF; Cost[s] = 0; inq[s] = 1;
    while (!Q.empty())
    {
        int k = Q.front(); Q.pop(); inq[k] = 0;
        for (int i = 0; i < v[k].size(); i++)
        {
            E e = edgs[v[k][i]];
            if (e.cap == e.flow) continue;
            if (Cost[e.to] < Cost[k] + e.cost)
            {
                from[e.to] = v[k][i];
                Cost[e.to] = Cost[k] + e.cost;
                a[e.to] = min(a[k],e.cap - e.flow);
                if (!inq[e.to]) inq[e.to] = 1,Q.push(e.to);
            }
        }
    }
    return a[t] != INF;
}

inline bool Check(DB now)
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            Add(i,j + n,1,A[i][j] - now * B[i][j]);
        Add(s,i,1,0); Add(i + n,t,1,0);
    }
    DB MaxCost = 0;
    while (SPFA())
    {
        MaxCost += (DB)(a[t]) * Cost[t];
        for (int z = t; z != s; z = edgs[from[z] ^ 1].to)
        {
            edgs[from[z]].flow += a[t];
            edgs[from[z] ^ 1].flow -= a[t];
        }
    }
    for (int i = s; i <= t; i++) v[i].clear();
    cnt = 0; return MaxCost > 0 || fabs(MaxCost) <= EPS;
}

int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
    #endif

    cin >> n; t = 2 * n + 1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            scanf("%lf",&A[i][j]);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            scanf("%lf",&B[i][j]);
    DB L = 0,R = 10000;
    while (R - L > 1E-7)
    {
        DB mid = (L + R) / 2.00;
        if (Check(mid)) L = mid; else R = mid;
    }
    printf("%.6lf\n",(L + R) / 2.00);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值