改了一下午,一直T。。。(蒟蒻在线求助)

P3355 骑士共存问题

Aurelian @ 2019-09-06 16:10:31

#include<bits/stdc++.h>

using namespace std;

const int N = 220,INF = 1 << 29;
int n,s,t,k,maxflow,tot = 1,sum;
int dx[8] = {1,1,-1,-1,2,2,-2,-2};
int dy[8] = {2,-2,2,-2,1,-1,1,-1};
int ver[N*N*20],edge[N*N*20],Next[N*N*20],head[N*N*20],v[N*N*20];

void add(int x,int y,int z){
     ver[++tot] = y;edge[tot] = z;Next[tot] = head[x];head[x] = tot;
     ver[++tot] = x;edge[tot] = 0;Next[tot] = head[y];head[y] = tot;
}

int num(int i,int j){
    return (i - 1)*n + j;
}
queue<int>q;
int d[N*N],cnt;
bool bfs(){
     cnt++;
     memset(d,0,sizeof(d));
     while(q.size()) q.pop();
     q.push(s);d[s] = 1;
     while(q.size()){
        int x = q.front();q.pop();
        for(int i = head[x];i;i = Next[i]){
            if(!edge[i]) continue;
            int y = ver[i];
            if(d[y]) continue;
            d[y] = d[x] + 1;
            q.push(y);
            if(y == t) return true;
         }
     }
     return false;
}

int dinic(int x,int flow){
    if(x == t) return flow;
    int rest = flow,k;
    for(int i = head[x];i;i = Next[i]){
        if(!edge[i]) continue;
        int y = ver[i];
        if(d[y] == d[x] + 1){
            k = dinic(y,min(rest,edge[i]));
            if(!k) d[y] = 0;
            edge[i] -= k;
            edge[i^1] += k;
            rest -= k;
        }
    }
    return flow - rest;
}

int main(){
    cin>>n>>k;
    sum = n*n - k;
    s = 0;t = n*n + 1;
    for(int i = 1;i <= k; i++){
        int a,b;
        scanf("%d%d",&a,&b);
        v[num(a,b)] = 1;
    }
    for(int i = 1;i <= n; i++)
       for(int j = 1;j <= n; j++){
          if(v[num(i,j)]) continue;
          if((i+j)%2 == 1){
            add(s,num(i,j),1);
            for(int p = 0;p < 8; p++){
                int nx = i + dx[p];
                int ny = j + dy[p];
                if(nx>=1&&ny>=1&&nx<=n&&ny<=n&&!v[num(nx,ny)])  add(num(i,j),num(nx,ny),INF);   
            }   
          }
          else add(num(i,j),t,1);
       }
    int flow = 0;
    while(bfs())
       while((flow = dinic(s,INF)))  maxflow += flow;
    printf("%d",sum - maxflow);
    return 0;
}

感觉没问题呀???


|