【牛客网】Whalyzh's Problem

每个\(b_{i,j}\)建一个点,认为选了\(b_{i,j}\)一定会选\(a_{i}\)和\(a_{j}\)

选了\(a_{i}\)的话会带了一个\(-b_{i,i}\)的价值

然后再用01分数规划二分答案,选了\(a_{i}\)还会带来\(-x\)的代价,x是二分的答案

如果正数值减最大流大于0认为这个答案可以达到

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
struct node {
int to,next;db cap;
}E[MAXN * 2];
int N,Ncnt,S,T;
int b[105][105],id[105][105];
int head[20005],sumE = 1,cur[20005];
void add(int u,int v,db c) {
E[++sumE].to = v;
E[sumE].next = head[u];
E[sumE].cap = c;
head[u] = sumE;
}
void addtwo(int u,int v,db c) {
add(u,v,c);add(v,u,0);
}
int dis[20005];
queue<int> Q;
bool BFS() {
memset(dis,0,sizeof(dis));
while(!Q.empty()) Q.pop();
Q.push(S);dis[S] = 1;
while(!Q.empty()) {
int u = Q.front();Q.pop();
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(E[i].cap > 1e-6 && !dis[v]) {
dis[v] = dis[u] + 1;
if(v == T) return true;
Q.push(v);
}
}
}
return false;
}
db dfs(int u,db aug) {
if(u == T) return aug;
for(int &i = cur[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(E[i].cap > 1e-6 && dis[v] == dis[u] + 1) {
db t = dfs(v,min(aug,E[i].cap));
if(t > 1e-6) {
E[i].cap -= t;
E[i ^ 1].cap += t;
return t;
}
}
}
return 0;
}
db Dinic() {
db res = 0;
while(BFS()) {
for(int i = 1 ; i <= Ncnt ; ++i) cur[i] = head[i];
while(db d = dfs(S,1e9) && d > 1e-6) res += d;
}
return res;
}
bool check(db x) {
db res = 0;
memset(head,0,sizeof(head));sumE = 1;
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= N ; ++j) {
addtwo(S,id[i][j],b[i][j]);res += b[i][j];
addtwo(id[i][j],i,1e9);
addtwo(id[i][j],j,1e9);
}
}
for(int i = 1 ; i <= N ; ++i) {
addtwo(i,T,b[i][i] + x);
}
return res - Dinic() > 1e-6;
}
void Solve() {
read(N); Ncnt = N;
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= N ; ++j) {
read(b[i][j]);
id[i][j] = ++Ncnt;
}
}
S = ++Ncnt;T = ++Ncnt;
db L = 0.0,R = 1e5;
int cnt = 50;
while(cnt--) {
db mid = (L + R) / 2.0;
if(check(mid)) L = mid;
else R = mid;
}
printf("%.5lf\n",L);
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
int T;
read(T);
for(int i = 1 ; i <= T ; ++i) Solve();
return 0;
}

【牛客网】Whalyzh's Problem的更多相关文章

  1. 牛客网第9场多校E(思维求期望)

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...

  2. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  3. Beautiful Numbers(牛客网)

    链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...

  4. 牛客网-乌龟跑步-(四维dfs)

    链接:https://ac.nowcoder.com/acm/problem/15294来源:牛客网 题目描述 有一只乌龟,初始在0的位置向右跑. 这只乌龟会依次接到一串指令,指令T表示向后转,指令F ...

  5. 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  6. 牛客网多校赛第七场--C Bit Compression【位运算】【暴力】

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524 ...

  7. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

  8. Running Median_via牛客网

    题目 链接:https://ac.nowcoder.com/acm/contest/28886/1002 来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 65536K, ...

  9. 牛客网 --java问答题

    http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...

随机推荐

  1. python qq发消息

    # 原理是先将需要发送的文本放到剪贴板中,然后将剪贴板内容发送到qq窗口 # 之后模拟按键发送enter键发送消息 import win32gui import win32con import win ...

  2. IDEA Junit FileNotFoundException: class path resource [spring/spring.xml] cannot be opened because it does not exist

    今天打算写一个单元测试,但是已经有写好的单元测试无论怎么弄都提示文件不存在,自己一度以为是启动方式不正确.这里简单记录一下处理过程 1 异常信息: Caused by: org.springframe ...

  3. shell 脚本同时对远程多台机器执行命令

    脚本1:需要机器之间免密 ssh-copy-id [-i [identity_file]] [user@]machine #!/bin/bash # ------------------------- ...

  4. Nginx之URL重写(rewrite)配置

    Nginx URL重写(rewrite)配置及信息详解1)if判断指令 语法为if(condition){…}     #对给定的条件condition进行判断.如果为真,大括号内的rewrite指令 ...

  5. The implementation of iterators in C# and its consequences (part 1) Raymond Chen

    Likeanonymous methods, iterators in C# are very complex syntactic sugar. You could do it all yoursel ...

  6. C#Winform ListView中没有Item双击事件的两种实现方法!

    第一种: //if (this.listView1.FocusedItem != null) //{ // if (this.listView1.SelectedItems != null) // { ...

  7. Python日志库logging总结-可能是目前为止将logging库总结的最好的一篇文章

    在部署项目时,不可能直接将所有的信息都输出到控制台中,我们可以将这些信息记录到日志文件中,这样不仅方便我们查看程序运行时的情况,也可以在项目出现故障时根据运行时产生的日志快速定位问题出现的位置. 1. ...

  8. Linux | Vim使用

    Linux vi/vim 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主 ...

  9. 数据库连接池配置(案例及排查指南) 原创: 有赞技术 有赞coder 4天前

    数据库连接池配置(案例及排查指南) 原创: 有赞技术 有赞coder 4天前

  10. osg #ifdef _WIN32 osg

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include <osgViewer/Viewer> #include ...