4 3 1 1111 1111 1111 LLLL LLLL LLLL 3 2 00000 01110 00000 ..... .LLL. ..... 3 1 00000 01110 00000 ..... .LLL. ..... 5 2 00000000 02000000 00321100 02000000 00000000 ........ ........ ..LLLL.. ........ ........
Case #1: 2 lizards were left behind. Case #2: no lizard was left behind. Case #3: 3 lizards were left behind. Case #4: 1 lizard was left behind.
分析:这题很明显的最大流模型,不过输出结果比较恶心。。。注意答案为1的情况啊,wa了两次。。。
构图:
将每个格子分为两个xa,xb,对于格子有人的,在源点与该点xa之间连一条容量为1的有向边,格子能跳出边界的,在该格子xb与汇点之间连一条容量为1的有向边,在xa与xb之间连一条容量为该格子数字的有向边,能互相到达的格子,在xbi与xaj之间连一条容量无穷的有向边
答案为总人数-最大流
代码:
#include<cstdio> #include<cstring> using namespace std; const int mm=66666; const int mn=810; const int oo=1000000000; const int dx[4]={1,-1,1,-1}; const int dy[4]={1,1,-1,-1}; int node,src,dest,edge; int reach[mm],flow[mm],next[mm]; int head[mn],work[mn],dis[mn],q[mn]; int mark[22][22]; char mapa[22][22],mapb[22][22]; inline int min(int a,int b) { return a<b?a:b; } inline void prepare(int _node,int _src,int _dest) { node=_node,src=_src,dest=_dest; for(int i=0; i<node; ++i)head[i]=-1; edge=0; } inline void addedge(int u,int v,int c) { reach[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; reach[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++; } bool Dinic_bfs() { int i,u,v,l,r=0; for(i=0; i<node; ++i)dis[i]=-1; dis[q[r++]=src]=0; for(l=0; l<r; ++l) for(i=head[u=q[l]]; i>=0; i=next[i]) if(flow[i]&&dis[v=reach[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0; } int Dinic_dfs(int u,int exp) { if(u==dest)return exp; for(int &i=work[u],v,tmp; i>=0; i=next[i]) if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0; } int Dinic_flow() { int i,ret=0,delta; while(Dinic_bfs()) { for(i=0; i<node; ++i)work[i]=head[i]; while(delta=Dinic_dfs(src,oo))ret+=delta; } return ret; } int main() { int i,j,k,l,f,x,y,n,m,t,d,num,tot,cas=0,ans; bool can; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&d); for(i=0; i<n; ++i)scanf("%s",mapa[i]); for(i=0; i<n; ++i)scanf("%s",mapb[i]); m=strlen(mapa[0]); for(tot=i=0; i<n; ++i) for(j=0; j<m; ++j) if(mapa[i][j]>'0')mark[i][j]=++tot; prepare(tot*2+2,0,tot*2+1); for(num=i=0; i<n; ++i) for(j=0; j<m; ++j) if(mapa[i][j]>'0') { addedge(mark[i][j],mark[i][j]+tot,mapa[i][j]-'0'); if(mapb[i][j]=='L')addedge(src,mark[i][j],1),++num; can=0; for(k=0;k<=d;++k) for(l=0;l<=d;++l) if(k+l>0&&k+l<=d) for(f=0;f<4;++f) { x=i+dx[f]*k; y=j+dy[f]*l; if(x>=0&&x<n&&y>=0&&y<m) { if(mapa[x][y]>'0')addedge(mark[i][j]+tot,mark[x][y],oo); } else can=1; } if(can)addedge(mark[i][j]+tot,dest,oo); } ans=num-Dinic_flow(); if(ans>1)printf("Case #%d: %d lizards were left behind.\n",++cas,ans); else if(ans)printf("Case #%d: %d lizard was left behind.\n",++cas,ans); else printf("Case #%d: no lizard was left behind.\n",++cas); } return 0; }