天天看點

bzoj 3232: 圈地遊戲

學過的知識早忘了,不會寫,又怕寫錯,果斷轉載:CQzhangyu

code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
const double eps=;
struct node{
    int x,y,next,other;
    double c;
}a[];int last[],len=;
int s[],h[],st,ed;
void ins(int x,int y,double c)
{
    int k1=++len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
    int k2=++len;
    a[len].x=y;a[len].y=x;a[len].c=;
    a[len].next=last[y];last[y]=len;
    a[k1].other=k2;a[k2].other=k1;
}
bool bt_h()
{
    memset(h,,sizeof(h));
    int l=,r=;s[l]=st;h[st]=;
    while(l!=r)
    {
        int x=s[l];
        for(int i=last[x];i;i=a[i].next)
        {
            int y=a[i].y;
            if(h[y]==&&a[i].c>) h[y]=h[x]+,s[r++]=y;
        }
        l++;
    }
    return h[ed]!=;
}
double findflow(int x,double f)
{
    if(x==ed) return f;
    double t,ans=;
    for(int i=last[x];i;i=a[i].next)
    {
        int y=a[i].y;
        if(h[x]+==h[y]&&a[i].c>&&ans<f)
        {
            ans+=(t=findflow(y,min(a[i].c,f-ans)));
            a[i].c-=t;a[a[i].other].c+=t;
        }
    }
    if(ans==) h[x]=;
    return ans;
}
int n,m,num=;
int A[][],ys[][],he[][],sh[][];
bool check(double X)
{
    memset(last,,sizeof(last));len=;
    double tot=;
    for(int i=;i<=n;i++)
        for(int j=;j<=m;j++)
        {
            int x=ys[i][j];
            ins(st,x,(double)A[i][j]);
            tot+=(double)A[i][j];
            ins(x,ys[i-][j],X*he[i][j]);
            ins(x,ys[i+][j],X*he[i+][j]);
            ins(x,ys[i][j-],X*sh[i][j]);
            ins(x,ys[i][j+],X*sh[i][j+]);
        }
    double ans=;
    while(bt_h()) ans+=findflow(st,);
    return tot-ans>;
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=;i<=n;i++)
        for(int j=;j<=m;j++) scanf("%d",&A[i][j]);
    for(int i=;i<=n+;i++)
        for(int j=;j<=m;j++) scanf("%d",&he[i][j]);
    for(int i=;i<=n;i++)
        for(int j=;j<=m+;j++) scanf("%d",&sh[i][j]);
    for(int i=;i<=n;i++)
        for(int j=;j<=m;j++) ys[i][j]=++num;
        st=;ed=++num;
    for(int i=;i<=n;i++) ys[i][]=ys[i][m+]=ed;
    for(int i=;i<=m;i++) ys[][i]=ys[n+][i]=ed;
    double l=,r=;
    while(l+eps<r)
    {
        double mid=(l+r)/;
        if(check(mid)) l=mid;
        else r=mid;
    }
    printf("%.3lf",l);
}