标签:include 任务 计算 cout gety bfs 输出 nbsp load
★★☆ 输入文件:knight.in
输出文件:knight.out
简单对比
时间限制:1 s 内存限制:128 MB
上某些方格设置了障碍,骑士不得进入。
3 3
knight.out
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; struct EdgeType { int v,e,f; }; struct EdgeType edge[50050*50]; const int dx[9]={0,-2,-1,1,2,2,1,-1,-2}; const int dy[9]={0,1,2,2,1,-1,-2,-2,-1}; int n,m,s=0,t,head[50050],cnt=1,ans,deep[50050]; char Cget; bool if_[205][205]; inline void in(int &now) { now=0,Cget=getchar(); while(Cget>‘9‘||Cget<‘0‘) Cget=getchar(); while(Cget>=‘0‘&&Cget<=‘9‘) { now=now*10+Cget-‘0‘; Cget=getchar(); } } inline void edge_add(int u,int v,int w) { edge[++cnt].v=v,edge[cnt].f=w,edge[cnt].e=head[u],head[u]=cnt; edge[++cnt].v=u,edge[cnt].f=0,edge[cnt].e=head[v],head[v]=cnt; } bool BFS() { queue<int>que; for(int i=s;i<=t;i++) deep[i]=-1; deep[s]=0,que.push(s); while(!que.empty()) { int now=que.front();que.pop(); for(int i=head[now];i;i=edge[i].e) { if(deep[edge[i].v]<0&&edge[i].f) { deep[edge[i].v]=deep[now]+1; if(edge[i].v==t) return true; que.push(edge[i].v); } } } return false; } int flowing(int now,int flow) { if(now==t||flow<=0) return flow; int oldflow=0; for(int i=head[now];i;i=edge[i].e) { if(edge[i].f<=0||deep[edge[i].v]!=deep[now]+1) continue; int pos=flowing(edge[i].v,min(flow,edge[i].f)); flow-=pos; oldflow+=pos; edge[i].f-=pos; edge[i^1].f+=pos; if(flow==0) return oldflow; } if(oldflow==0) deep[now]=-1; return oldflow; } int main() { freopen("knight.in","r",stdin); freopen("knight.out","w",stdout); in(n),in(m); ans=n*n-m; t=n*n+1;int x,y; while(m--) { in(x),in(y); if_[x][y]=true; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(if_[i][j]) continue; if((i+j)%2==0) { edge_add(s,(i-1)*n+j,1); for(int v=1;v<=8;v++) { if(i+dx[v]>0&&i+dx[v]<=n&&j+dy[v]>0&&j+dy[v]<=n) { if(!if_[i+dx[v]][j+dy[v]]) { edge_add((i-1)*n+j,(i+dx[v]-1)*n+j+dy[v],0x7ffffff); } } } } else edge_add((i-1)*n+j,t,1); } } while(BFS()) ans-=flowing(s,0x7ffffff); cout<<ans; return 0; }
标签:include 任务 计算 cout gety bfs 输出 nbsp load
原文地址:http://www.cnblogs.com/IUUUUUUUskyyy/p/6502124.html