网络流怎么卡常啊。。。

P3355 骑士共存问题

Rainbow_qwq @ 2019-12-05 15:07:00

TLE 81 分,求指教。

https://www.luogu.com.cn/record/28117244

#pragma GCC diagnostic error "-std=c++11"
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}
#define maxn 500000
#define inf 0x3f3f3f3f
int n,m,s,t,sum,maxflow;
int dep[maxn];
bool vis[1005][1005];
int tot=1,head[maxn];
struct edge{
    int to,nxt,w;
}e[maxn];
inline void adde(int u,int v,int w){
    e[++tot]=(edge){v,head[u],w};
    head[u]=tot;
}
inline void add(int u,int v,int w){adde(u,v,w);adde(v,u,0);}
bool bfs(int s,int t){
    queue<int>q;
    memset(dep,63,sizeof dep);
    dep[s]=0;q.push(s);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=head[u];i;i=e[i].nxt)
        {
            int v=e[i].to;
            if(dep[v]==inf&&e[i].w){
                dep[v]=dep[u]+1;
                q.push(v);
            }
        }
    }return dep[t]<inf;
}
int dfs(int u,int t,int minn)
{
    if(!minn||u==t)return minn;
    int res=0;
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(dep[v]!=dep[u]+1)continue;
        int flow=dfs(v,t,min(minn,e[i].w));
        if(!flow)continue;
        res+=flow,minn-=flow,e[i].w-=flow,e[i^1].w+=flow;
        if(!minn)break;
    }return res;
}
inline int dinic(int s,int t){int res=0;while(bfs(s,t))res+=dfs(s,t,inf);return res;}
inline int P(int x,int y){return (x-1)*n+y;}
int dx[15]={-1,-2,-2,-1,+1,+2,+2,+1};
int dy[15]={-2,-1,+1,+2,+2,+1,-1,-2};
void getedge()
{
    s=0,t=n*n+1;
    For(i,1,n)
        For(j,1,n){
            if(vis[i][j])continue;
            if((i+j)%2){
                add(s,P(i,j),1);
                For(k,0,7){
                    int xx=i+dx[k],yy=j+dy[k];
                    if(xx>=1&&yy>=1&&xx<=n&&yy<=n)add(P(i,j),P(xx,yy),1);
                }
            }else add(P(i,j),t,1);
        }
}
signed main()
{
    n=read(),m=read();
    For(i,1,m){int x=read(),y=read();vis[x][y]=1;}
    getedge();cout<<n*n-m-dinic(s,t);
    return 0;
}

by TEoS @ 2019-12-05 15:19:59

我觉得是您代码有问题 我没卡常一个dinic就过了还贼快


by VenusM1nT @ 2019-12-05 15:26:29

弧优化一下吧


by 1saunoya @ 2019-12-05 15:51:19

HLPP 或者 弧优化


by Freopen @ 2019-12-05 16:07:55

当前弧优化不加必死啊


|