• bzoj3175: [Tjoi2013]攻击装置&&4808: 马


    终于知道为啥网络流这么受欢迎了。

    其实就是构个图模板一下的事儿,比较好打是吧。

    然后这题网络流黑白染色(其实感觉上匈牙利更加直接好想啊,但是实际上黑白染色给人感觉就是二分图)

    st连白而ed连黑,流量为1

    不能同时出现的就建无限流量的边

    然后sum-最小割

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int dx[8]={-2,-2,-1,1,2,2,1,-1};
    const int dy[8]={-1,1,2,2,1,-1,-2,-2};
    
    struct node
    {
        int x,y,c,next,other;
    }a[410000];int len,last[51000];
    void ins(int x,int y,int c)
    {
        int k1,k2;
        
        len++;k1=len;
        a[len].x=x;a[len].y=y;a[len].c=c;
        a[len].next=last[x];last[x]=len;
        
        len++;k2=len;
        a[len].x=y;a[len].y=x;a[len].c=0;
        a[len].next=last[y];last[y]=len;
        
        a[k1].other=k2;
        a[k2].other=k1;
    }
    
    
    int st,ed,h[51000],list[51000];
    bool bt_h()
    {
        memset(h,0,sizeof(h));h[st]=1;
        int head=1,tail=2;list[1]=st;
        while(head!=tail)
        {
            int x=list[head];
            for(int k=last[x];k;k=a[k].next)
            {
                int y=a[k].y;
                if(a[k].c>0&&h[y]==0)
                {
                    h[y]=h[x]+1;
                    list[tail]=y;
                    tail++;
                }
            }
            head++;
        }
        if(h[ed]==0)return false;
        return true;
    }
    int findflow(int x,int f)
    {
        if(x==ed)return f;
        int s=0;
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(a[k].c>0&&h[y]==h[x]+1&&s<f)
            {
                int t=findflow(y,min(a[k].c,f-s));
                s+=t;a[k].c-=t;a[a[k].other].c+=t;
            }
        }
        if(s==0)h[x]=0;
        return s;
    }
    
    int n,color[210][210];
    char ss[210];
    bool mp[210][210];
    int point(int x,int y){return (x-1)*n+y;}
    int main()
    {
        scanf("%d",&n);
        int br;
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",ss+1);
            for(int j=1;j<=n;j++)
            {
                if(ss[j]=='0')mp[i][j]=true, sum++;
                else           mp[i][j]=false;
            }
        }
        //-----sc-----
        memset(color,0,sizeof(color));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(j==1)color[i][j]=1-color[i-1][j];
                else     color[i][j]=1-color[i][j-1];
        //---paint_color---
            
        //----init-----
        
        st=n*n+1,ed=n*n+2;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(mp[i][j]==true)
                {
                    if(color[i][j]==0)
                        ins(st,point(i,j),1);
                    else
                        ins(point(i,j),ed,1);
                }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(mp[i][j]==true&&color[i][j]==0)
                    for(int t=0;t<=7;t++)
                    {
                        int ti=i+dx[t],tj=j+dy[t];
                        if(ti>0&&ti<=n&&tj>0&&tj<=n&&mp[ti][tj]==true)
                            ins(point(i,j),point(ti,tj),999999999);
                    }
            
        //----composition------
        
        int ans=0;
        while(bt_h()==true)
        {
            ans+=findflow(st,999999999);
        }
        printf("%d
    ",sum-ans);
        return 0;
    }
  • 相关阅读:
    Djano restframework
    python测试一
    SQL分类,DDL,DML,DCL
    sql查询时,根据特定的条件给表的某一个字段赋值
    数据类型之Nullable
    web.config节点
    拼凑的宿主-host
    css的优先级
    jquery——write less,do more
    double类型计算
  • 原文地址:https://www.cnblogs.com/AKCqhzdy/p/8573144.html
Copyright © 2020-2023  润新知