BZOJ 4823 Luogu P3756 [CQOI2017]老C的方块 (网络流、最小割)

橙三吉。 提交于 2019-12-01 10:05:14

题目链接

(Luogu) https://www.luogu.org/problem/P3756
(BZOJ) http://lydsy.com/JudgeOnline/problem.php?id=4823

题解

有点神仙的最小割题。
考虑题目里的图形,如果我们用四种颜色对棋盘进行染色,奇数行依次染\(0,1,2,3,0,1,2,3...\), 偶数行依次染\(3,2,1,0,3,2,1,0...\)则条件可以转化为不能出现相连的\(4\)个颜色互不相同的块。
那么可以建一个四层的图,对于每条两侧都有关键点的特殊边,按照\(S\rightarrow 3\rightarrow 0\rightarrow 1\rightarrow 2\rightarrow T\)的顺序连边,其中\(S\rightarrow 3\)\(3\)色点的点权,\(0\rightarrow 1\)连两个关键点权值的最小值,\(2\rightarrow T\)\(2\)色点的点权。不出现相连的四个颜色互不相同的块等价于不存在从\(S\)\(T\)的路径。
然后跑最小割即可。
因为是分层图,所以dinic跑得很快(复杂度应该是\(O(n\sqrt n)\)),可以通过此题。

代码

#include<bits/stdc++.h>
#define llong long long
using namespace std;

const int INF = 1e9;
namespace NetFlow
{
    const int N = 1e5+2;
    const int M = 8e5;
    struct Edge
    {
        int v,w,nxt,rev;
    } e[(M<<1)+3];
    int fe[N+3];
    int te[N+3];
    int dep[N+3];
    int que[N+3];
    int n,en,s,t;
    void addedge(int u,int v,int w)
    {
//      printf("addedge %d %d %d\n",u,v,w);
        en++; e[en].v = v; e[en].w = w;
        e[en].nxt = fe[u]; fe[u] = en; e[en].rev = en+1;
        en++; e[en].v = u; e[en].w = 0;
        e[en].nxt = fe[v]; fe[v] = en; e[en].rev = en-1;
    }
    bool bfs()
    {
        for(int i=1; i<=n; i++) dep[i] = 0;
        int head = 1,tail = 1; que[1] = s; dep[s] = 1;
        while(head<=tail)
        {
            int u = que[head]; head++;
            for(int i=fe[u]; i; i=e[i].nxt)
            {
                int v = e[i].v;
                if(e[i].w>0 && dep[v]==0)
                {
                    dep[v] = dep[u]+1;
                    if(v==t)return true;
                    tail++; que[tail] = v;
                }
            }
        }
        return false;
    }
    int dfs(int u,int cur)
    {
        if(u==t||cur==0) {return cur;}
        int rst = cur;
        for(int &i=te[u]; i; i=e[i].nxt)
        {
            int v = e[i].v;
            if(e[i].w>0 && rst>0 && dep[v]==dep[u]+1)
            {
                int flow = dfs(v,min(rst,e[i].w));
                if(flow>0)
                {
                    e[i].w -= flow; 
                    rst -= flow;
                    e[e[i].rev].w += flow;
                    if(rst==0) {return cur;}
                }
            }
        }
        if(rst==cur) {dep[u] = -2;}
        return cur-rst;
    }
    int dinic(int _n,int _s,int _t)
    {
        n = _n,s = _s,t = _t;
        int ret = 0;
        while(bfs())
        {
            for(int i=1; i<=n; i++) te[i] = fe[i];
            memcpy(te,fe,sizeof(int)*(n+1));
            ret += dfs(s,INF);
        }
        return ret;
    }
}
using NetFlow::addedge;
using NetFlow::dinic;

const int N = 1e5;
struct Point
{
    int x,y,w;
} a[N+3];
map<int,int> mp[N+3];
int id[N+3],clr[N+3];
int n,nx,ny;

int getclr(int x,int y)
{
    if(y&1) {return (x-1)&3;}
    else {return 3-((x-1)&3);}
}

int main()
{
    scanf("%d%d%d",&nx,&ny,&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
        mp[a[i].x][a[i].y] = i;
    }
    for(int i=1; i<=n; i++)
    {
        int x = a[i].x,y = a[i].y,clr = getclr(x,y);
        if(((x+(y<<1))&3)==3 && mp[x+1].count(y))
        {
            int j = mp[x+1][y],w = min(a[i].w,a[j].w);
            if(clr==0)
            {
                int k = mp[x-1][y]; if(k) {addedge(k+2,i+2,INF);}
                k = mp[x][y-1]; if(k) {addedge(k+2,i+2,INF);}
                k = mp[x][y+1]; if(k) {addedge(k+2,i+2,INF);}
                k = mp[x+2][y]; if(k) {addedge(j+2,k+2,INF);}
                k = mp[x+1][y+1]; if(k) {addedge(j+2,k+2,INF);}
                k = mp[x+1][y-1]; if(k) {addedge(j+2,k+2,INF);}
                addedge(i+2,j+2,w);
            }
            else if(clr==1)
            {
                int k = mp[x+2][y]; if(k) {addedge(k+2,j+2,INF);}
                k = mp[x+1][y+1]; if(k) {addedge(k+2,j+2,INF);}
                k = mp[x+1][y-1]; if(k) {addedge(k+2,j+2,INF);}
                k = mp[x-1][y]; if(k) {addedge(i+2,k+2,INF);}
                k = mp[x][y-1]; if(k) {addedge(i+2,k+2,INF);}
                k = mp[x][y+1]; if(k) {addedge(i+2,k+2,INF);}
                addedge(j+2,i+2,w);
            }
        }
        else if(clr==3)
        {
            addedge(1,i+2,a[i].w);
        }
        else if(clr==2)
        {
            addedge(i+2,2,a[i].w);
        }
    }
    int ans = dinic(n+2,1,2);
    printf("%d\n",ans);
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!