BZOJ4819 新生舞会
4819: [Sdoi2017]新生舞会
Time Limit: 10 Sec Memory Limit: 128 MB
Description
Input
Output
Sample Input
19 17 16
25 24 23
35 36 31
9 5 6
3 4 2
7 8 9
Sample Output
题解
分数规划 + 网络流
二分C值,设当前C值为C', 每对人对答案的贡献为\( a - b * C' \),费用流使贡献总和最大,为正则说明C'可行。
使用迭代会使效率更高,每次费用流都是在找一个尽量更优或可行的答案,所以我们把费用流跑出的方案记录下来,作为下一次的C'值。
每次迭代结束比较上一次的C'与现在跑出来的C'',差的绝对值小于eps则找到最优解,C'的初始值任意。
代码
/* Stay hungry, stay foolish. */
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<vector>
#include<set>
#include<sstream>
#include<stack>
#include<ctime>
#include<cmath>
#include<cctype>
#include<climits>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<bitset>
#include<complex>
using namespace std;
/*
#define getchar() getc()
char buf[1<<15],*fs,*ft;
inline char getc() {return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin)),fs==ft)?0:*fs++;}
*/
template <class _T> inline void read(_T &_x) {
int _t; bool _flag=false;
while((_t=getchar())!='-'&&(_t<''||_t>'')) ;
if(_t=='-') _flag=true,_t=getchar(); _x=_t-'';
while((_t=getchar())>=''&&_t<='') _x=_x*+_t-'';
if(_flag) _x=-_x;
}
typedef long long LL;
const int maxn = ;
const int maxm = ;
const double eps = 1e-;
struct Edge {
int v, from, nxt, flow; double cost;
Edge() {}
Edge(int a, int b, int c, double d): v(a), nxt(b), flow(c), cost(d) {}
}e[maxm];
int fir[maxn], ecnt, pre[maxn], preu[maxn];
double dis[maxn]; bool vis[maxn];
inline void addedge(int a, int b, int c, double d) {
e[++ecnt] = Edge(b, fir[a], c, d), fir[a] = ecnt;
e[++ecnt] = Edge(a, fir[b], , -d), fir[b] = ecnt;
}
int n, sink, a[maxn][maxn], b[maxn][maxn];
inline bool spfa() {
memset(dis, 0xfe, sizeof (double) * (sink + ));
memset(vis, false, sizeof (bool) * (sink + ));
double DMIN = dis[];
queue<int> q; dis[] = , q.push();
while (!q.empty()) {
int now = q.front(); q.pop(); vis[now] = false;
for (int u = fir[now]; u; u = e[u].nxt) if (e[u].flow) {
if (dis[e[u].v] < dis[now] + e[u].cost) {
dis[e[u].v] = dis[now] + e[u].cost;
pre[e[u].v] = now, preu[e[u].v] = u;
if (!vis[e[u].v]) {
vis[e[u].v] = true;
q.push(e[u].v);
}
}
}
}
return dis[sink] != DMIN;
}
double augment() {
int now = sink;
while (now != ) {
e[preu[now]].flow -= ;
e[preu[now] ^ ].flow += ;
now = pre[now];
}
return dis[sink];
}
double mcmf() {
double ret = ;
while (spfa()) {
ret += augment();
}
return ret;
}
double Run(double ans) {
ecnt = ;
memset(fir, , sizeof (int) * (sink + ));
for (int i = ; i <= n; ++i) {
for (int j = ; j <= n; ++j) {
addedge(i, j + n, , a[i][j] - ans * b[i][j]);
}
}
for (int i = ; i <= n; ++i) addedge(, i, , );
for (int i = ; i <= n; ++i) addedge(n + i, sink, , );
mcmf();
int ansa = , ansb = ;
for (int i = ; i <= n; ++i) {
for (int u = fir[i]; u; u = e[u].nxt) {
if (e[u].flow == && e[u].v > n && e[u].v < sink) {
ansa += a[i][e[u].v - n], ansb += b[i][e[u].v - n];
}
}
}
return (double)ansa / ansb;
}
int main() {
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
read(n); sink = n + n + ;
for (int i = ; i <= n; ++i) for (int j = ; j <= n; ++j)
read(a[i][j]);
for (int i = ; i <= n; ++i) for (int j = ; j <= n; ++j)
read(b[i][j]);
double prev, ans = ;
do {
prev = ans, ans = Run(ans);
} while (fabs(ans - prev) > eps);
printf("%.6lf", ans);
return ;
}
BZOJ4819 新生舞会的更多相关文章
- BZOJ-4819: 新生舞会(01分数规划+费用流)
Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买一个男生和一个女生一起跳舞,互为舞伴.Cathy收集了这些同学之间 ...
- 【BZOJ4819】[Sdoi2017]新生舞会 01分数规划+费用流
[BZOJ4819][Sdoi2017]新生舞会 Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买一个男生和一个女 ...
- 【BZOJ4819】新生舞会(分数规划,网络流)
[BZOJ4819]新生舞会(分数规划,网络流) 题面 BZOJ Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买 ...
- bzoj4819 [Sdoi2017]新生舞会
Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会买一个男生和一个女生一起跳舞,互为舞伴.Cathy收集了这些同学之间的 ...
- 【BZOJ4819】【SDOI2017】新生舞会 [费用流][分数规划]
新生舞会 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 学校组织了一次新生舞会,Cathy ...
- [BZOJ4819][SDOI2017]新生舞会(分数规划+费用流,KM)
4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1097 Solved: 566[Submit][Statu ...
- 【bzoj4819】[Sdoi2017]新生舞会 分数规划+费用流
题目描述 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会买一个男生和一个女生一起跳舞,互为舞伴.Cathy收集了这些同学之间的关系,比如两个 ...
- BZOJ4819 [Sdoi2017]新生舞会 【01分数规划 + 费用流】
题目 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买一个男生和一个女生一起跳舞,互为舞伴.Cathy收集了这些同学之间的关系,比如两个人 ...
- BZOJ4819: [Sdoi2017]新生舞会(01分数规划)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1029 Solved: 528[Submit][Status][Discuss] Descripti ...
随机推荐
- Sprint2-2.0
1.开始一个新的冲刺: 起止:2016.6.1~2016.6.14 按照以下过程进行 ProductBacklog:继续向下细化 Sprint 计划会议:确定此次冲刺要完成的目标 Sprint Bac ...
- HDU 2106 母猪的故事
http://acm.hdu.edu.cn/showproblem.php?pid=2160 Problem Description 话说现在猪肉价格这么贵,著名的ACBoy 0068 也开始了养猪生 ...
- HDU 2086 A1 = ?
http://acm.hdu.edu.cn/showproblem.php?pid=2086 Problem Description 有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci ( ...
- [转帖]什么是TRIM与GC?他们是怎样让SSD保持高速的
什么是TRIM与GC?他们是怎样让SSD保持高速的 2017-7-6 15:43 | 作者:Strike | 关键字:SSD,TRIM,GC,超能课堂 分享到 SSD的写入方式决 ...
- 安装iproute2
yum install -y gcc bison flex #tar -xzf iproute2-3.17.0.tar.gz #cd iproute2-3.17.0#sed -i '/^TARGET ...
- MySQL复制 -- 复制出错怎么办?
假如我们生产环境复制出错?该怎么办呢? 下面提供几种办法: 1. 手工处理,补回不一致数据(可以利用主库来补数据.也可以利用binlog来补数据) 2.用开源工具来解决一致性问题 3.自己造轮子,解决 ...
- R1 学习记录
libevent框架学习特点: 1.可移植行,跨平台的 2.速度快,libevent会用各平台最快的非阻塞IO函数 3.扩展性 4.方便性构成: 1.evutil: 抽象出各平台network的函数 ...
- BZOJ 2251: [2010Beijing Wc]外星联络
2251: [2010Beijing Wc]外星联络 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 795 Solved: 477[Submit][ ...
- 【BZOJ1034】泡泡堂(贪心)
[BZOJ1034]泡泡堂(贪心) 题面 BZOJ 洛谷 题解 很基础的贪心,然而我竟然没写对...身败名裂. 大概就是类似田忌赛马. 先拿看当前最大值是否能否解决对面最大值,否则检查能否用最小值来兑 ...
- 20165218 《网络对抗技术》Exp2 后门原理与实践
Exp2 后门原理与实践 准备工作 1. 查看Linux和Win的IP地址,ping通 Linux地址 Win7地址 ping 2.下载ncat并装载到win7主机 3.下载socat并装载到win7 ...