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 ...
随机推荐
- Internet History, Technology and Security (Week 5-2)
Week 5 (续) Layer 2: Internet Protocol The InterNetwork (IP) 老师强调了一下不用去记住他介绍的人所说的每句话,而是记住要点,了解老师所做的PP ...
- 9th 学习博客:使用Codebloks实现C++的图形化界面
使用开发工具codeblocks,添加ResEdit.exe这个控件,可以很方便地进行图形化编辑,这是在网上找得教程,实现的是最基本的在对话框内添加按钮,并实现单击响应在控制台输出相应的文字. mai ...
- js中的php rand函数
//文件rand.js function MyRand(min, max){ this.min = min; this.max = max; } MyRand.prototype.getRand = ...
- Fastqc使用说明
用FastQC检查二代测序原始数据的质量 2013-01-28 21:28:10| 分类: Bioinformatics | 标签:bioinformatics deep-seq |举报 | ...
- 惭愧, eclipse 之 build path
算下来大学到现在已近用了很久的 eclipse 了, 包括 myeclipse, 但是今天碰到的问题让我很惭愧, 一个老项目的编译都搞了好久. 环境: Myeclipse 6.X Struts 1.X ...
- UVA11624_Fire!
在一个矩形方阵里面,一个人要从一个位置走向另一个位置,其中某些地方有火源,每过一分钟,火源就会点燃相邻的点,同时相邻的点也变成了火源.人不能通过有火的点.问一个人能够安全地走到目的地去?最短时间多少? ...
- spring注解方式注入
1.通过Resource注入 1.在属性上注入 1.默认注入 即不指定spring容器里面的名字 匹配规则:先通过属性的名字查找 再通过属性类型与实现类类型匹配查找 当有两个实现类会报错 2.通过指定 ...
- linux系统日志__ratelimit: N callbacks suppressed
报错 今天线上遇到故障,php进行因为段错误退出了,系统日志中的kernel报错如下: Feb 25 22:25:11 web_server_01 kernel: __ratelimit: 250 c ...
- SpringMVC框架并发时出现id变成另外一个用户id问题
今天测试写的代码,出现了在用一个账户登录操作的时候,操作记录的是另外一个id. 经过查找网上的解决方案确认了问题:在controller里面定义了一个userid属性,每次都通过userid传输值.然 ...
- Candies CodeForces - 991C(二分水题)
就是二分暴力就好了 为什么要记下来 呵呵....emm你说为什么... 行吧 好吧 我一直以为我的二分出问题了 原来不是 依旧很帅 统计的时候求的减了多少次 然后用次数乘了mid 这样做会使那个人获 ...