M_sea

洛谷4001 [ICPC-Beijing 2006]狼抓兔子
LuoguBZOJ分析显然要求的是最小割啊qwq建出图跑一遍Dinic就行了qwq然而我加上当前弧优化后,跑得比不...
扫描右侧二维码阅读全文
08
2019/01

洛谷4001 [ICPC-Beijing 2006]狼抓兔子

Luogu

BZOJ

分析

显然要求的是最小割啊qwq

建出图跑一遍Dinic就行了qwq

然而我加上当前弧优化后,跑得比不加还慢

代码

//It is made by M_sea
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#define re register
using namespace std;

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int MAXN=1000000+10;
const int INF=2147483647;

struct Edge { int v,w,nxt; };
Edge e[MAXN*6];
int head[MAXN],cnt=0;
int cur[MAXN];

inline void addEdge(int u,int v,int c) {
    e[cnt].v=v,e[cnt].w=c,e[cnt].nxt=head[u],head[u]=cnt++;
    e[cnt].v=u,e[cnt].w=c,e[cnt].nxt=head[v],head[v]=cnt++;
}

int level[MAXN];
int n,m,s,t;

inline int Dinic_Bfs() {
    memset(level,0,sizeof(level));
    queue<int> Q; Q.push(s); level[s]=1;
    while (!Q.empty()) {
        int u=Q.front(); Q.pop();
        for (re int i=head[u];i!=-1;i=e[i].nxt) {
            int v=e[i].v;
            if (!level[v]&&e[i].w) {
                level[v]=level[u]+1;
                Q.push(v);
            }
        }
    }
    return level[t];
}

inline int Dinic_Dfs(int u,int cpflow) {
    if (u==t) return cpflow;
    int addflow=0;
    for (re int i=head[u];i!=-1&&addflow<cpflow;i=e[i].nxt) {
        int v=e[i].v;
        if (level[v]==level[u]+1&&e[i].w) {
            int tmpadd=Dinic_Dfs(v,min(cpflow-addflow,e[i].w));
            e[i].w-=tmpadd; e[i^1].w+=tmpadd;
            addflow+=tmpadd;
        }
    }
    if (!addflow) level[u]=0; //玄学优化
    return addflow;
}

inline int Dinic() {
    int maxflow=0;
    while (Dinic_Bfs()) maxflow+=Dinic_Dfs(s,INF);
    return maxflow;
}

inline int id(int x,int y) { return m*(x-1)+y; }

int main() {
    memset(head,-1,sizeof(head));
    n=read(),m=read(),s=1,t=n*m;
    for (re int i=1;i<=n;++i)
        for (re int j=1;j<m;++j)
            addEdge(id(i,j), id(i,j+1), read());
    for (re int i=1;i<n;++i)
        for (re int j=1;j<=m;++j)
            addEdge(id(i,j), id(i+1,j), read());
    for (re int i=1;i<n;++i)
        for (re int j=1;j<m;++j)
            addEdge(id(i,j), id(i+1,j+1), read());
    printf("%d\n",Dinic());
    return 0;
}
最后修改:2019 年 01 月 21 日 05 : 33 PM

1 条评论

  1. smy

    %%%%%%%%%%%%吊打我

发表评论