#2003. 「SDOI2017」新生舞会

内存限制:256 MiB时间限制:1500 ms标准输入输出
题目类型:传统评测方式:文本比较
上传者: 匿名

题目描述

学校组织了一次新生舞会,Cathy 作为经验丰富的老学姐,负责为同学们安排舞伴。

有 n nn 个男生和 n nn 个女生参加舞会,一个男生和一个女生一起跳舞,互为舞伴。
Cathy 收集了这些同学之间的关系,比如两个人之前是否认识,计算得出 ai,j a_{i, j}a​i,j​​,表示第 i ii 个男生和第 j jj 个女生一起跳舞时他们喜悦程度。
Cathy 还需要考虑两个人一起跳舞是否方便,比如身高体重差别会不会太大,计算得出 bi,j b_{i, j}b​i,j​​ 表示第 i ii 个男生和第 j jj 个女生一起跳舞时的不协调裎度。

当然,还需要考虑很多其他间题。

Cathy 想先用一个程序通过 ai,j a_{i, j}a​i,j​​ 和 bi,j b_{i, j}b​i,j​​ 求出一种方案,再手动对方案进行微调。
Cathy 找到你,希望你帮她写那个程序。

一个方案中有 n nn 对舞伴,假设每对舞伴的喜悦程度分别是 a1′,a2′,…,an′ a'_1, a'_2, \ldots, a'_na​1​′​​,a​2​′​​,…,a​n​′​​,假设每对舞伴不协调程度分别是 b1′,b2′,…,bn′ b'_1, b'_2, \ldots, b'_nb​1​′​​,b​2​′​​,…,b​n​′​​。令

C=a1′+a2′+⋯+an′b1′+b2′+⋯+bn′ C = \frac{a'_1 + a'_2 + \cdots + a'_n}{b'_1 + b'_2 + \cdots + b'_n}C=​b​1​′​​+b​2​′​​+⋯+b​n​′​​​​a​1​′​​+a​2​′​​+⋯+a​n​′​​​​

Cathy 希望 C 值最大。

输入格式

第一行一个整数 n nn。
接下来 n nn 行,每行 n nn 个正整数,第 i ii 行第 j jj 个数表示 ai,j a_{i, j}a​i,j​​。
接下来 n nn 行,每行 n nn 个正整数,第 i ii 行第 j jj 个数表示 bi,j b_{i, j}b​i,j​​。

输出格式

一行一个数,表示 C CC 的最大值。四舍五入保留六位小数,选手输出的小数需要与标准输出相等。

样例

样例输入

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

样例输出

5.357143

数据范围与提示对于 10% 10\%10% 的数据,1≤n≤5 1 \leq n \leq 51≤n≤5;

对于 40% 40\%40% 的数据,1≤n≤18 1 \leq n \leq 181≤n≤18;
另外存在 20% 20\%20% 的数据,bi,j=1 b_{i, j} = 1b​i,j​​=1;
对于 100% 100\%100% 的数据,1≤n≤100,1≤ai,j≤104,1≤bi,j≤104 1 \leq n \leq 100, 1 \leq a_{i, j} \leq 10 ^ 4,1 \leq b_{i, j} \leq 10 ^ 41≤n≤100,1≤a​i,j​​≤10​4​​,1≤b​i,j​​≤10​4​​。

传送一篇来自苣苣的博客:01分数规划总结

感觉01分数规划就是先二分答案,然后再求最值。

题目链接:https://loj.ac/problem/2003

题意:选出n对舞伴,使得价值和/代价和最大。

思路:基础01分数规划+最大权匹配(可以使用最小费用流求解,先对费用取负数,最后答案再取负数)

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
const double eps=1e-;
int a[maxn][maxn],b[maxn][maxn];
struct edge
{
int from,to;
int c;
int a,b;
};
vector<edge>es;
vector<int>G[maxn];
double dist[maxn];
int pre[maxn];
void addedge(int u,int v,int c,int a,int b)
{
es.push_back((edge)
{
u,v,c,a,b
});
es.push_back((edge)
{
v,u,,-a,-b
});
int x=es.size();
G[u].push_back(x-);
G[v].push_back(x-);
}
bool SPFA(int s,int t,double mid)
{
static std::queue<int> q;
static bool inq[maxn];
for(int i=; i<maxn; i++) dist[i]=1.0*inf,inq[i]=false;
pre[s]=-;
dist[s]=0.0;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=false;
for(int i=; i<G[u].size(); i++)
{
edge e=es[G[u][i]];
double d=0.0;
if(e.a&&e.b) d=1.0*e.a-mid*e.b;
if(e.c&&dist[e.to]>dist[u]-d)
{
pre[e.to]=G[u][i];
dist[e.to]=dist[u]-d;
if(!inq[e.to]) q.push(e.to),inq[e.to]=true;
}
}
}
return fabs(inf-dist[t])>eps;
}
double dinic(int s,int t,int f,double mid)
{
int flow=;
double cost=0.0;
while(SPFA(s,t,mid))
{
int d=f;
for(int i=t; i!=s; i=es[pre[i]].from)
d=min(d,es[pre[i]].c);
f-=d;
flow+=d,cost+=d*dist[t];
for(int i=t; i!=s; i=es[pre[i]].from)
{
es[pre[i]].c-=d;
es[pre[i]^].c+=d;
}
if(f<=) break;
}
for(int i=; i<es.size(); i+=) es[i].c=,es[i+].c=;
//cout<<-1*cost<<" ******"<<endl<<endl;
return -*cost;
}
bool check(double mid,int s,int t,int n)
{
return dinic(s,t,n,mid)>=eps?true:false;
}
int main()
{
int n,m;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
scanf("%d",&a[i][j]);
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
scanf("%d",&b[i][j]);
}
int s=,t=*n+;
for(int i=; i<=n; i++) addedge(s,i,,,);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
addedge(i,j+n,,a[i][j],b[i][j]);
}
for(int i=; i<=n; i++) addedge(i+n,t,,,);
double l=0.0,r=1000000000.0;
double ans=0.0;
while((r-l)>eps)
{
double mid=(l+r)/;
//printf("%.8f %.8f %.8f\n",l,r,mid);
if(check(mid,s,t,n)) l=mid,ans=l;
else r=mid;
}
printf("%.6f\n",ans);
return ;
}

基础01分数规划+最大权匹配

最新文章

  1. 适配iOS10的哪些事 ---- 学习笔记八
  2. BZOJ 1101: [POI2007]Zap
  3. 网络爬虫2--PHP/CURL库(client URL Request Library)
  4. 我的J2EE学习历程
  5. Feature hashing相关 - 2
  6. DataTables - 问题集
  7. gerrit
  8. IOS源码封装成.bundle和.a文件,以及加入xib的具体方法,翻遍网络,仅此一家完美翻译!! IOS7!!(3) 完美结局
  9. 【C语言模拟实现】浮点数-转-定点数
  10. JS之正则表达式验证URL
  11. 【基础】多线程更新窗体UI的若干方法
  12. OpenGL绘制棱锥,剔除
  13. XML 字符串解析
  14. hdu1312 Red and Black 简单BFS
  15. 使用Spring-boot小结
  16. ORACLE复杂查询之连接查询
  17. codechef Chef And Easy Xor Queries
  18. Python 运维
  19. unity3d生命周期
  20. xilinx AXI相关IP核学习

热门文章

  1. JQuery 基本知识
  2. 学习BOS物流项目第九天
  3. Java中的BigDecimal类精度问题
  4. 用pandas读取excel报错
  5. Shader基础(固定管线着色器)
  6. python内置函数getattr用法
  7. C/s程序过时了吗?
  8. java面试题:分布式
  9. centos 6 下KVM 安装学习之旅
  10. ZOJ2018/4月月赛G题Traffic Light(广搜)