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 ...
随机推荐
- how to show video in website
how to show video in website old version browsers https://www.computerhope.com/issues/ch000591.htm h ...
- BZOJ3203 SDOI2013保护出题人(三分)
给a做一个前缀和,那么现在每次所查询的就是(sn-sk)/(bn+nd-(k+1)d)的最大值.这个式子可以看成是(bn+nd,sn)和((k+1)d,sk)所成直线的斜率. 脑补一条直线不断减小斜率 ...
- 【刷题】BZOJ 1934 [Shoi2007]Vote 善意的投票
Description 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可 ...
- NetApp常用巡检命令
常用检查命令 environment status 查看环境信息 version 查看OS版本 sysconfig -v 查看系统信息(设备序列号 系统软.硬件信息等) sysconfig -a 查看 ...
- docker attach 和 docker exec
docker attach docker attach -- Attach to a running container. 常用选项: --sig-proxy=true:Proxy all recei ...
- bzoj2300【HAOI2011】防线修建
题目描述 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上层现在还犹豫不决,到底该把哪些城市作为保护对象呢?又由于 ...
- 【最大流/二分图匹配】【网络流24题】【P3254】 圆桌问题
Description 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,--,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,--,n) ...
- 【题解】Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths Codeforces 741D DSU on Tree
Prelude 很好的模板题. 传送到Codeforces:(* ̄3 ̄)╭ Solution 首先要会DSU on Tree,不会的看这里:(❤ ω ❤). 众所周知DSU on Tree是可以用来处 ...
- Python3 笨方法 练习41(面向对象)详解及运行结果
#无尽模式训练你,检验所掌握的面向对象的单词和短语. import random from urllib.request import urlopen import sys WORD_URL = &q ...
- Linux基础命令之grep
grep : 根据pattern(模式)搜索文本,并将符合模式的文本行显示出来,并不会修改原文件. 用法: grep [options] ‘ pattern ‘ /file #也可 ...