【牛客网】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;
}

最新文章

  1. Oozie_示例
  2. python实验二:字符串排序
  3. ansible写一个简单的playbook
  4. MySQL_关于用嵌套表计算的可以不用 20161205
  5. 高吞吐量的分布式发布订阅消息系统Kafka--spring-integration-kafka的应用
  6. HDOJ-1052 田忌赛马(贪心)
  7. Vue2 全家桶仿 微信App 项目,支持多人在线聊天和机器人聊天
  8. Java-认识变量、注释并能及时发现错误
  9. TETELaser Cutting System 不连续吹起的问题
  10. python实现查有道词典
  11. Python机器学习 (Python Machine Learning 中文版 PDF)
  12. 微服务架构中APIGateway原理
  13. 关于 IIS7.0下文件写入无权限的解决办法
  14. Eclipse添加JDK,JRE切换
  15. What is AMQP? and the architecture
  16. Qt环境配置 + Qt使用教程
  17. Two Sum(II和IV)
  18. Laravel Carbon 简明使用
  19. linux fack 文件系统修复命令
  20. php AES-128-CBC 加密 通信java

热门文章

  1. 使用Redis sorted set实现集合设置member过期
  2. android通用的UUID唯一标示符
  3. 会话技术cookie与session
  4. openstack 基镜像展开过程
  5. 2019-8-15C#MDI窗体实现多窗口效果
  6. android studio: 9:57 Unsupported Modules Detected: Compilation is not supported for following modules: map, app, ota, MediaEditor, rcLcmSercive, DroneSDK, qrcodelibrary, rcService, speechService. Unfo
  7. Java设计模式: 单例模式
  8. Java 13新特性
  9. osg 3ds模型加载与操作
  10. osg HUD 前景色