【牛客网】Whalyzh's Problem

【牛客网】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;
}
posted @ 2019-06-21 09:45  sigongzi  阅读(273)  评论(0编辑  收藏  举报