【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收集了这些同学之间的 ...
随机推荐
- Windows 7系统启动MongoDB失败解决办法?
问题现象: 1.在配置Python环境安装MongoDB时发现在“服务”里面手动启动失败,报错如下: 2.在cmd里面也无法启动,注意这里要以管理员身份启动cmd哦 问题解决: 1.需要先在bin下执 ...
- 退出全屏监听ESC事件
fullscreenchange事件 fullscreenchange:当窗口大小改变时触发 isFullscreen:全局变量 window.addEventListener("fulls ...
- 工具类-vim在shell中卡死的情况
time:2015/11/35 在xshell下面使用vim编辑,有时候会出现突然卡死的情况.但是如果重新开一个终端的话,打开文件又是一大堆问题,今天又碰到了,搜了一下就找到一个帮助了[1] 原因:按 ...
- 统计过程控制与评价 Cpk、SPC、PPM
Cpk(Process capability index)--工序能力指数 SPC(Statisical Process Control)--工艺过程统计受控状态分析 PPM(Parts Per Mi ...
- September 27th 2017 Week 39th Wednesday
We both look up at the same stars, yet we see such different things. 我们仰望同一片星空,却看见了不同的事物. Looking up ...
- November 28th 2016 Week 49th Monday
You only live once, but if you do it right, once is enough. 年华不虚度,一生也足矣. One today can win two tomor ...
- 通过Python实现一个文档的半自动录入工具
需求出现/使用场景: 因为公司需要将word办的接口文档在线化,看起来是个很好的事情,但是就是苦逼了我们这些干活的,其中工程量最大的就是参数的录入,要是参数少也罢,有的接口动辄三四十个参数,更甚八九十 ...
- [李居丽][다이아몬드][Diamond]
歌词来源:http://music.163.com/#/song?id=484056974 作曲 : Damon Sharpe/JOHN HO/JEFF SHUM [作曲 : Damon Sharpe ...
- angular 生命周期钩子 ngOnInit() 和 ngAfterViewInit() 的区别
angular 生命周期钩子的详细介绍在 https://angular.cn/guide/lifecycle-hooks 文档中做了介绍. ngOnInit() 在 Angular 第一次显示数据 ...
- SGU---103 最短路变形
题目链接: https://cn.vjudge.net/problem/SGU-103#author=ECUST_FZL 题目大意: Dingiville 城市的交通规则非常奇怪,城市公路通过路口相连 ...