【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收集了这些同学之间的 ... 
随机推荐
- python处理excel(一):读
			功能:读取一个excel里的第2个sheet,将该sheet的内容全部输出. #coding=utf8 import xlrd def read_excel(): workbook = xlrd.op ... 
- SVN合并时报错:Merge tracking not allowed with missing subtrees; try restoring these items
			使用的是TortoiseSVN; Merge tracking not allowed with missing subtrees; try restoring these items 下面会有跟着几 ... 
- 使用uwsgi发布项目
			1.先下载 uwsgi 指定豆瓣源下载 pip install -i https://pypi.douban.com/simple uwsgi 2.查看你的uwsgi基于那个python解释器运行的 ... 
- 生成器-yield初接触
			什么是生成器? 生成器的实质就是迭代器 在python中有三种方式来获取生成器 1. 通过生成器函数 2. 通过各种推导式实现生成器 3. 通过数据的转换也可以获取生成器 将函数中的return换成y ... 
- zabbix日常监控Apache2.4
			Apache的安装请参考https://www.cnblogs.com/huangyanqi/p/9168637.html 1.修改配置 [root@apache ~]# httpd -v Serve ... 
- dev  richEditControl控件 设置文字 字体 大小
			Document doc = NoticeContentRichEditControl.Document; doc.BeginUpdate(); doc.Text = "需要设置格式的文字& ... 
- right here waiting的歌词
			right here waiting的歌词 2006-12-30 17:36 匿名 | 分类:音乐 | 该问题已经合并到>> right here waiting的歌词有吗? 扫描二维 ... 
- 【原创】uwsgi中多进程+多线程原因以及串行化accept() - thunder_lock说明
			如有不对,请详细指正. 最近再研究uwsgi如何部署python app,看uwsgi的文档,里面有太多的参数,但每个参数的解释太苍白,作为菜鸟的我实在是不懂.想搞清楚uwsgi的工作原因以及里面的一 ... 
- Hadoop HA on Yarn——集群启动
			这里分两部分,第一部分是NameNode HA,第二部分是ResourceManager HA (ResourceManager HA是hadoop-2.4.1之后加上的) NameNode HA 1 ... 
- css背景精华所在+前端页面开发流程
			background属性 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个设置项: ... 
