刚学c++,求dalao帮忙查错

P3355 骑士共存问题

partychicken @ 2018-08-04 20:51:57

81分,两个大数据过不去。。。

貌似是死循环了,在本机跑了40min没出答案

#include<bits/stdc++.h>

using namespace std;

const int N=40010;
const int inf=0x3f3f3f3f;

const int dx[]={1,2,2,1,-1,-2,-2,-1};
const int dy[]={2,1,-1,-2,-2,-1,1,2};

struct Edge
{
    int to,nxt,val;

    Edge():nxt(-1){}
}e[N<<4];
int head[N],cnt(-1);

void addedge(int u,int v,int val)
{
    e[++cnt].to=v;
    e[cnt].val=val;
    e[cnt].nxt=head[u];
    head[u]=cnt;
}

void insedge(int u,int v,int val)
{
    addedge(u,v,val);
    addedge(v,u,0);
}

int s,t;

queue<int>q;
int dpth[N];
bool bfs()
{
    while(!q.empty())
    {
        q.pop();
    }
    memset(dpth,0,sizeof(dpth));

    dpth[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=head[now];~i;i=e[i].nxt)
        {
            if(!dpth[e[i].to]&&e[i].val)
            {
                dpth[e[i].to]=dpth[now]+1;
                q.push(e[i].to);
            }
        }
    }
    return (bool)dpth[t];
}

int dfs(int now,int fl)
{
    if(now==t||fl==0)
    {
        return fl;
    }

    int di=0;
    for(int i=head[now];~i;i=e[i].nxt)
    {
        int vs=e[i].to;
        if(dpth[vs]==dpth[now]+1)
        {
            int tmp=dfs(vs,min(fl-di,e[i].val));
            //if(!tmp)dpth[vs]=0;
            e[i].val-=tmp;
            e[i^1].val+=tmp;
            di+=tmp;
        }
    }
    return di;
}

int dinic()
{
    int res=0;
    while(bfs())
    {
        res+=dfs(s,inf);
    }
    return res;
}

int mp[210][210];

int n,m;
int geid(int i,int j)
{
    return (i-1)*n+j;
}

int main()
{
    memset(head,-1,sizeof(head));

    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {       
        int x,y;
        cin>>x>>y;
        mp[x][y]=1;
    }

    s=n*n+100;
    t=n*n+200;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(mp[i][j])
            {
                continue;
            }
            else
            {
                int u=geid(i,j);
                if((i+j)&1)
                {
                    insedge(s,u,1);
                }
                else
                {
                    insedge(u,t,1);
                    continue;
                }
                for(int p=0;p<8;p++)
                {
                    int nowx=i+dx[p];
                    int nowy=j+dy[p];
                    if(nowx>0&&nowy>0&&nowx<=n&&nowy<=n&&(!mp[nowx][nowy]))
                    {
                        int v=geid(nowx,nowy);
                        insedge(u,v,inf);
                    }
                }
            }
        }
    }
    cout<<n*n-m-dinic()<<endl;
}

by p_b_p_b @ 2018-08-04 20:56:50

563AC是刚学c++?那我岂不是没入门...


by shiys2007 @ 2018-08-04 20:58:03

@partychicken 刚学c++做省选难度?


by kdlkswb @ 2018-08-04 20:58:55

您好,请问您为什么要强调您刚学C++呢


by Carbon @ 2018-08-04 20:59:19

TQL


by Carbon @ 2018-08-04 20:59:21


by bztMinamoto @ 2018-08-04 21:01:23

刚学c++AC500多……%%%大佬


by 权御天下 @ 2018-08-04 21:02:07

您好,请问您为什么要强调您刚学C++呢


by shiys2007 @ 2018-08-04 21:02:16

@bztMinamoto ++


by 本居小铃 @ 2018-08-04 21:02:54

orz


by lemir3 @ 2018-08-04 21:15:58

刚学C++就学网络流 %%%


| 下一页