TLE?0分求助

P3355 骑士共存问题

gdjcwsk @ 2020-08-21 19:07:37

RT,死活看不出来那里超时了

#include <bits/stdc++.h>
using namespace std;
const int maxn=100005;
const long long inf=100000000005;
int m,n,s,t,tot=1,heads[maxn];
bool mp[205][205];
int x[8]={1,1,-1,-1,2,2,-2,-2},y[8]={2,-2,2,-2,1,-1,1,-1}; 
int dep[maxn];
queue<int>q;
struct hh
{
    int to,nxt,val;
}g[10000005];
void add(int u,int v,int w)
{
    g[++tot].to=v;
    g[tot].val=w;
    g[tot].nxt=heads[u];
    heads[u]=tot;
}
bool bfs()
{
    memset(dep,0,sizeof(dep));
    dep[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=heads[x];i!=-1;i=g[i].nxt)
        {
            if(g[i].val>0&&!dep[g[i].to])
            {
                dep[g[i].to]=dep[x]+1;
                q.push(g[i].to);
            }
        }
    }
    if(dep[t]!=0)
    {
        return 1;
    }
    return 0;
}
int dfs(int u,int dist)
{
    if(u==t)
    {
        return dist;
    }
    for(int i=heads[u];i!=-1;i=g[i].nxt)
    {
        if(dep[g[i].to]==dep[u]+1&&g[i].val!=0)
        {
            int di=dfs(g[i].to,min(dist,g[i].val));
            if(di>0)
            {
                g[i].val-=di;
                g[i^1].val+=di;
                return di;
            }
        }
    }
    return 0;
}
int dinic()
{
    int ans=0;
    while(bfs())
    {
        while(int d=dfs(s,inf))
        {
            ans+=d;
        }
    }
    return ans;
}
int main()
{
    cin>>n>>m;
    t=n*n+1;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        mp[x][y]=1;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if((i+j)&1)
            {
                if(!mp[i][j])
                {
                    add(s,(i-1)*n+j,1);
                    add((i-1)*n+j,s,0);
                }
            }
            else
            {
                if(!mp[i][j])
                {
                    add((i-1)*n+j,t,1);
                    add(t,(i-1)*n+j,0);
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(((i+j)&1)==0)
            {
                continue;
            }
            for(int k=0;k<8;k++)
            {
                int xx=i+x[k],yy=j+y[k]; 
                if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&!mp[xx][yy])
                {
                    add((i-1)*n+j,(xx-1)*n+yy,inf);
                    add((xx-1)*n+yy,(i-1)*n+j,0);
                }
            }
        }
    }
    cout<<n*n-m-dinic();
}

by ___balalida___ @ 2020-08-21 19:18:35

dinic不是这么写的


by ___balalida___ @ 2020-08-21 19:19:39

int dinic()
{
    int ans=0;
    while(bfs())
    {
        while(int d=dfs(s,inf))//这个
        {
            ans+=d;
        }
    }
    return ans;
}

去掉那个while


by Zxx200611 @ 2020-08-21 19:21:34

@balalida ???为什么要去掉 while


by gdjcwsk @ 2020-08-21 19:22:08

@balalida 这个貌似没啥用吧


by Zxx200611 @ 2020-08-21 19:24:05

@gdjcwsk dfs 函数应该不是这么写的,建议再学学 Dinic。


by gdjcwsk @ 2020-08-21 19:24:38

@Zxx200611 我再看看试试,毕竟dinic对我来说也不那么熟。


by ___balalida___ @ 2020-08-21 19:27:17

我的是这么写的:https://www.luogu.com.cn/paste/nr1bbunv


by gdjcwsk @ 2020-08-21 19:32:35

代码明显有点问题吧。。


by gdjcwsk @ 2020-08-21 19:55:11

A了,仔细观察一番确有些问题


|