BZOJ 3232: 圈地游戏 (分数规划+最小割)

https://www.lydsy.com/JudgeOnline/problem.php?id=3232

转化成最大权闭合子图的问题

二分答案x 分数规划
将源点与每个点相连 容量为点权
将每个点与相邻点相连 容量为x*公共边边权
将边界点与汇点相连 容量为x*外边权(可以理解为即使选了这些点也要割掉这些边)

这样建图可以大概理解成选了一个点而相邻点没有选的话就一定要把它连出去的边割掉
也就是要用线围起来

注意容量答案什么的要全部改成double
按值域初始化r 当然这个r是取不到的

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define pb push_back
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define ansn() printf("%d\n",ans)
#define lansn() printf("%lld\n",ans)
#define r0(i,n) for(int i=0;i<(n);++i)
#define r1(i,e) for(int i=1;i<=e;++i)
#define rn(i,e) for(int i=e;i>=1;--i)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define lowbit(a) (a&(-a))
#define all(a) a.begin(),a.end()
#define pii pair
#define pll pair
#define mp(aa,bb) make_pair(aa,bb)
#define lrt rt<<1
#define rrt rt<<1|1
#define X first
#define Y second
#define PI (acos(-1.0))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007 ;
const double eps=1e-9;
const int inf=0x3f3f3f3f;
//const ll infl = 100000000000000000;//1e17
const int maxn=  2e5+20;
const int maxm = 2e5+20;
//muv[i]=(p-(p/i))*muv[p%i]%p;
int in(int &ret) {
    char c;
    int sgn ;
    if(c=getchar(),c==EOF)return -1;
    while(c!='-'&&(c<'0'||c>'9'))c=getchar();
    sgn = (c=='-')?-1:1;
    ret = (c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9')ret = ret*10+(c-'0');
    ret *=sgn;
    return 1;
}

int head[maxn];
struct node {
    int u,v;
    double cap;
    int nt;
    node() {}
    node(int u,int v,double cap,int nt):u(u),v(v),cap(cap),nt(nt) {}
} edge[maxm];
int cnt;
void addedge(int u,int v,double cap) {
    edge[cnt] = node(u,v,cap,head[u]);
    head[u] = cnt++;
    edge[cnt] = node(v,u,0,head[v]);
    head[v] = cnt++;
}
int dis[maxn];
int cur[maxn];
bool bfs(int s,int t) {
    mst(dis,-1);
    queue<int>q;
    dis[s] = 0;
    q.push(s);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i=head[u]; ~i; i=edge[i].nt) {
            node no = edge[i];
            int v = no.v;
            double cap = no.cap;
            if(cap>1e-6&&dis[v]==-1) {
                dis[v]=dis[u]+1;
                if(v==t)return 1;
                q.push(v);
            }
        }
    }
    return dis[t]!=-1;
}
double dfs(int u,double mx,int t) {
    if(u==t)return mx;
    double used = 0;
    for(int i = cur[u]; ~i; i = edge[i].nt) {
        cur[u] = i;
        node &no = edge[i];
        int v = no.v;
        double cap = no.cap;
        if(dis[v]==dis[u]+1&&cap>1e-6) {
            double w = mx - used;
            double flow = dfs(v,min(w,cap),t);
            edge[i].cap-=flow;
            edge[i^1].cap+=flow;
            used+=flow;
            if(fabs(used - mx)<1e-6)return used;
        }
    }
    if(used<1e-6)dis[u] = -1;
    return used;
}
int dinic(int s,int t,int tot) {
    double ans = 0;
    while(bfs(s,t)) {
        double flow;
        r0(i,tot)cur[i] = head[i];
        while((flow = dfs(s,1.0*inf,t))>eps)
            ans += flow;
    }
    return ans;
}
int a[123][123];
int col[123][123];
int row[123][123];
int n,m;
int id(int x,int y) {
    return (x-1)*m+y;
}
double sum = 0;
bool check(double x) {
    cnt = 0;
    mst(head,-1);
    r1(i,n)r1(j,m) {
        addedge(0,id(i,j),1.0*a[i][j]);
        if(i==1)addedge(id(i,j),n*m+1,x*row[0][j]);
        else if(i==n)addedge(id(i,j),n*m+1,x*row[n][j]);

        if(j==1)addedge(id(i,j),n*m+1,x*col[i][0]);
        else if(j==m)addedge(id(i,j),n*m+1,x*col[i][m]);

        if(i>1)addedge(id(i-1,j),id(i,j),x*row[i-1][j]),addedge(id(i,j),id(i-1,j),x*row[i-1][j]);

        if(j>1)addedge(id(i,j-1),id(i,j),x*col[i][j-1]),addedge(id(i,j),id(i,j-1),x*col[i][j-1]);
    }
    double q = dinic(0,n*m+1,n*m+2);
    return sum-q>1e-6;
}
int main() {
#ifdef LOCAL
    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
#endif // LOCAL
//    int n,m;
    sdd(n,m);
    r1(i,n)r1(j,m)sd(a[i][j]),sum+=1.0*a[i][j];
    r0(i,n+1)r1(j,m)sd(row[i][j]);
    r1(i,n)r0(j,m+1)sd(col[i][j]);
    double l = 0, r = n*m*100.0;
    double ep = 1e-6;
    while(r-l>ep) {
        double mid = (l+r)/2;
        if(check(mid))l = mid ;
        else r = mid;
    }
    double ans = l;
    printf("%.3f",ans);
    return 0;
}

你可能感兴趣的:(网络流,分数规划)