30分求助

P3355 骑士共存问题

览遍千秋 @ 2019-08-12 14:44:16

RT,又WA又T。。。

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

int n,m,S,T,xx,yy;
int a[203][203],ans;
int Head[40003],Next[100003],to[100003],w[100003],tot=1;
int dx[8]={-1,-2,-2,-1, 1, 2, 2, 1};
int dy[8]={-2,-1, 1, 2,-2,-1, 1, 2};
int d[203];
template <typename Tp>
void read(Tp &x){
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-'){
        fh=-1;ch=getchar();
    }
    else fh=1;
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    x*=fh;
}

bool bfs(){
    memset(d,0,sizeof(d));
    queue<int>q;q.push(S);d[S]=1;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=Head[x];i;i=Next[i]){
            if(d[to[i]]||!w[i]) continue;
            q.push(to[i]);d[to[i]]=d[x]+1;
            if(to[i]==T) return true;
        }
    }
    return false;
}

int dfs(int x,int flow){
    if(x==T) return flow;
    int rest=flow;
    for(int i=Head[x];i&&rest;i=Next[i]){
        if(d[to[i]]!=d[x]+1||!w[i]) continue;
        int k=dfs(to[i],min(rest,w[i]));
        if(!k) d[to[i]]=0;
        else{
            w[i]-=k,w[i xor 1]+=k;
            rest-=k;
        }
    }
    return flow-rest;
}

void add(int x,int y,int z){
    to[++tot]=y,Next[tot]=Head[x],Head[x]=tot,w[tot]=z;
}

int main(){
    read(n);read(m);S=n*n+1,T=S+1;//错误笔记:此处原写作S=n*m+1,错误区分n,m 

    for(register int i=1;i<=m;i++){
        read(xx);read(yy);
        a[xx][yy]=1;
    }

    for(register int i=1;i<=n;i++){
        for(register int j=1;j<=n;j++){
            if(a[i][j]) continue;
            if((i+j)&1){
                add(S,(i-1)*n+j,1);add((i-1)*n+j,S,0);
            }
            else{
                add((i-1)*n+j,T,1);add(T,(i-1)*n+j,0);
            }
        }
    }

    for(register int i=1;i<=n;i++){
        for(register int j=1;j<=n;j++){
            if(a[i][j]) continue;
            for(int k=0;k<8;k++){
                xx=i+dx[k],yy=j+dy[k];//错误笔记:误将dx,dy写作d 
                if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&(!a[xx][yy])){
                    add((i-1)*n+j,(xx-1)*n+yy,1);add((xx-1)*n+yy,(i-1)*n+j,0);
                }
            }
        }
    }
    int tt=0;
    while(bfs()){
        while((tt=dfs(S,0x3f3f3f3f))) ans+=tt;
    }
    printf("%d\n",n*n-ans-m);
    return 0;
}

by 览遍千秋 @ 2019-08-12 14:49:32

啊呸,27分求助


by lzyqwq @ 2022-05-14 22:14:40

@expect2004 同求


|