【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收集了这些同学之间的 ...
随机推荐
- [翻译] ZCSHoldProgress
ZCSHoldProgress 以下是使用效果: https://github.com/zshannon/ZCSHoldProgress "Your users be pressin' lo ...
- 应用程序-特定 权限设置并未向在应用程序容器 不可用 SID (不可用)中运行的地址 LocalHost (使用 LRPC) 中的用户
这是安装biztalk server 2013出现的问题,很多天了没解决,下边这个解决办法也搜到过类似的,但上次实验时出现设置组件权限时发现都是按钮都是灰的,无法操作. 这次设置好了.谢谢ibg. 文 ...
- Shell中, 退出整个脚本
常规做法 cat >test.sh<<EOF'' #!/bin/bash exit_script(){ exit 1 } echo "before exit" e ...
- file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known
请求页面时候报错 file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not know ...
- 原生JS 将canvas生成图片
核心代码: <script type="text/javascript"> // Converts image to canvas; returns new canva ...
- 项目属性的target platform和target platform version到底是什么(vs2015开发windows驱动小记)
根据官方对属性页的介绍(General Property Page (Project))可了解: target platform是build后的结果会跑在哪个平台,例如windows,android, ...
- BZOJ1001: [BeiJing2006]狼抓兔子【最短路+对偶图】
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Se ...
- macbook下 go 语言的 helloworld
go语言开发的目录 一般go语言$GOPATH 目录约定有三个子目录: src 存放源代码(比如:.go .c .h .s等) pkg 编译后生成的文件(比如:.a) bin 编译后生成的可执行文件( ...
- Js 运行机制 event loop
Js - 运行机制 (Even Loop) Javascript 的单线程 - 引用思否的说法: JavaScript的一个语言特性(也是这门语言的核心)就是单线程.什么是单线程呢?简单地说就是同一时 ...
- JavaScript里的创建对象(一)
一.序 面向对象有一个标志,那就是它们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.ECMA-262把对象定义为“无序属性的集合,其属性可以包含基本值.对象或者函数”. 使用Obje ...