【SDOI2017】新生舞会
题面
题解
一眼\(0/1\)分数规划
二分答案\(mid\),我们要\(\sum\limits_i a^{'}_i - mid\sum\limits_i b_i^{'}\)最大
那么我们将\(a_{i,j}-mid\times b_{i,j}\)作为\((i,j)\)的边权
跑一遍二分图最大权匹配即可。
代码
// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<queue>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int N(110), maxn(5e5 + 10);
const double eps(1e-8);
struct edge { int next, to, cap; double dis; } e[maxn];
int head[maxn], e_num = -1, S, T, a[N][N], b[N][N];
int pre[maxn], pre_e[maxn], vis[maxn], n;
double dis[maxn], cost;
inline void add_edge(int from, int to, int cap, double dis)
{
e[++e_num] = (edge) {head[from], to, cap, dis}; head[from] = e_num;
e[++e_num] = (edge) {head[to], from, 0, -dis}; head[to] = e_num;
}
std::queue<int> q;
void MinCostMaxFlow()
{
cost = 0;
while(1)
{
std::fill(dis + S, dis + T + 1, -1e20); clear(vis, 0);
vis[S] = 1, dis[S] = 0, q.push(S);
while(!q.empty())
{
int x = q.front(); q.pop();
for(RG int i = head[x]; ~i; i = e[i].next)
{
int to = e[i].to; double ds = e[i].dis + dis[x];
if(e[i].cap > 0 && dis[to] < ds)
{
dis[to] = ds, pre[to] = x, pre_e[to] = i;
if(!vis[to]) vis[to] = 1, q.push(to);
}
}
vis[x] = 0;
}
if(dis[T] == -1e20) return;
int cap = 1e9;
for(RG int i = T; i ^ S; i = pre[i])
cap = std::min(cap, e[pre_e[i]].cap);
cost += 1. * cap * dis[T];
for(RG int i = T; i ^ S; i = pre[i])
e[pre_e[i]].cap -= cap, e[pre_e[i] ^ 1].cap += cap;
}
}
bool check(double mid)
{
e_num = -1, S = 1, T = (n << 1) + 2;
for(RG int i = S; i <= T; i++) head[i] = -1;
for(RG int i = 1; i <= n; i++) add_edge(S, i + 1, 1, 0);
for(RG int i = n + 2; i < T; i++) add_edge(i, T, 1, 0);
for(RG int i = 1; i <= n; i++) for(RG int j = 1; j <= n; j++)
add_edge(i + 1, j + n + 1, 1, 1. * a[i][j] - 1. * b[i][j] * mid);
MinCostMaxFlow(); return fabs(cost) <= eps || cost > eps;
}
int main()
{
n = read();
for(RG int i = 1; i <= n; i++)
for(RG int j = 1; j <= n; j++)
a[i][j] = read();
for(RG int i = 1; i <= n; i++)
for(RG int j = 1; j <= n; j++)
b[i][j] = read();
double l = -1e7, r = 1e7;
while(fabs(r - l) > eps)
{
double mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
printf("%.6lf\n", r);
return 0;
}
【SDOI2017】新生舞会的更多相关文章
- [Sdoi2017]新生舞会 [01分数规划 二分图最大权匹配]
[Sdoi2017]新生舞会 题意:沙茶01分数规划 貌似\(*10^7\)变成整数更科学 #include <iostream> #include <cstdio> #inc ...
- BZOJ_4819_[Sdoi2017]新生舞会_01分数规划+费用流
BZOJ_4819_[Sdoi2017]新生舞会_01分数规划+费用流 Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞 ...
- 洛谷 P3705 [SDOI2017]新生舞会 解题报告
P3705 [SDOI2017]新生舞会 题目描述 学校组织了一次新生舞会,\(Cathy\)作为经验丰富的老学姐,负责为同学们安排舞伴. 有\(n\)个男生和\(n\)个女生参加舞会买一个男生和一个 ...
- 【BZOJ 4819】 4819: [Sdoi2017]新生舞会 (0-1分数规划、二分+KM)
4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 601 Solved: 313 Description 学校 ...
- 【BZOJ4819】[Sdoi2017]新生舞会 01分数规划+费用流
[BZOJ4819][Sdoi2017]新生舞会 Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买一个男生和一个女 ...
- [BZOJ4819][SDOI2017]新生舞会(分数规划+费用流,KM)
4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1097 Solved: 566[Submit][Statu ...
- 【算法】01分数规划 --- HNOI2009最小圈 & APIO2017商旅 & SDOI2017新生舞会
01分数规划:通常的问法是:在一张有 \(n\) 个点,\(m\) 条边的有向图中,每一条边均有其价值 \(v\) 与其代价 \(w\):求在图中的一个环使得这个环上所有的路径的权值和与代价和的比率最 ...
- 4819: [Sdoi2017]新生舞会(分数规划)
4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1031 Solved: 530[Submit][Statu ...
- 题解:SDOI2017 新生舞会
题解:SDOI2017 新生舞会 Description 学校组织了一次新生舞会,Cathy 作为经验丰富的老学姐,负责为同学们安排舞伴. 有 \(n\) 个男生和 \(n\) 个女生参加舞会.一个男 ...
- bzoj4819 [Sdoi2017]新生舞会
Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会买一个男生和一个女生一起跳舞,互为舞伴.Cathy收集了这些同学之间的 ...
随机推荐
- LinkedHashSet 元素唯一,存储取出有序
package cn.itcast_04; import java.util.LinkedHashSet; /* * LinkedHashSet:底层数据结构由哈希表和链表组成. * 哈希表保证元 ...
- UVa 10213 - How Many Pieces of Land ?(欧拉公式)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Hive学习之路 (十七)Hive分析窗口函数(五) GROUPING SETS、GROUPING__ID、CUBE和ROLLUP
概述 GROUPING SETS,GROUPING__ID,CUBE,ROLLUP 这几个分析函数通常用于OLAP中,不能累加,而且需要根据不同维度上钻和下钻的指标统计,比如,分小时.天.月的UV数. ...
- Day15 集合(二)
Set简介 定义 public interface Set<E> extends Collection<E> {} Set是一个继承于Collection的接口,即Set也是集 ...
- ethereumjs/ethereumjs-common-2-API文档
https://github.com/ethereumjs/ethereumjs-common/blob/master/docs/index.md 该API的调用的详细例子可见ethereumjs/e ...
- node-webkit,nwjs 系统托盘【Tray】实践
参照自:https://www.cnblogs.com/xuanhun/p/3678943.html Tray包含title.tooltip.icon.menu.alticon五个属性. title属 ...
- [转]Apache HTTP Server 与 Tomcat 的三种连接方式介绍
首先我们先介绍一下为什么要让 Apache 与 Tomcat 之间进行连接.事实上 Tomcat 本身已经提供了 HTTP 服务,该服务默认的端口是 8080,装好 tomcat 后通过 8080 端 ...
- VC++获取当前路径及程序名的实现代码
VC上或取当前路径有多种方法,最常用的是使用 GetCurrentDirectory和GetModuleFileName函数,个中都有诸多注意事项,特别总结一下 一.获取当前运行目录的绝对路径 1.使 ...
- _tcschr和_tcsrchr使用
好处:是可以不管是用unicode 编码还是其他 ,代码都不用改. C++标准库函数提供了字符和字符串的操作函数,并提供了其UNICODE版本,如: 1._tcschr代替strchr或者wcschr ...
- Qt在线/离线安装包下载网址和说明
截至到2018年3月27日,Qt最新版本的安装需要使用在线安装工具:qt-unified-windows-x86-3.0.2-online.exe,该安装个工具的下载地址为: http://downl ...