题意:
给出一个地图,地图里面有一些青蛙,每个位置有限定承受的次数,如果青蛙跳跃到某个位置,那么这个位置的承受力就减一,如果承受力是0就无法到达这个位置。现在问最少有多少青蛙无法跳出地图。
题解:
将有承受力的点拆点,然后网络流。TL中。。
#include<iostream> #include<math.h> #include<stdio.h> #include<algorithm> #include<string.h> #include<vector> #include<queue> #include<map> #include<set> using namespace std; #define B(x) (1<<(x)) typedef long long ll; void cmax(int& a,int b){ if(b>a)a=b; } void cmin(int& a,int b){ if(b<a)a=b; } const int oo=0x3f3f3f3f; const int MOD=1000000007; const int maxn=2200; const int maxm=300000; struct EDGE{ int v,next,c,f; }E[maxm<<1]; int head[maxn],tol; int gap[maxn],dep[maxn],pre[maxn],cur[maxn]; void Init(){ memset(head,-1,sizeof head); tol=0; } void add_edge(int u,int v,int f,int rf=0){ E[tol].v=v; E[tol].c=f; E[tol].f=0; E[tol].next=head[u]; head[u]=tol++; E[tol].v=u; E[tol].c=rf; E[tol].f=0; E[tol].next=head[v]; head[v]=tol++; } int isap(int start,int end,int N){ memset(gap,0,sizeof gap); memset(dep,0,sizeof dep); memcpy(cur,head,sizeof head); int u=start; pre[u]=-1; gap[0]=N; int ans=0; while(dep[start]<N){ if(u==end){ int Min=oo; for(int i=pre[u];i!=-1;i=pre[E[i^1].v]) if(Min>E[i].c-E[i].f) Min=E[i].c-E[i].f; for(int i=pre[u];i!=-1;i=pre[E[i^1].v]){ E[i].f+=Min; E[i^1].f-=Min; } u=start; ans+=Min; continue; } bool flag=false; int v; for(int i=cur[u];i!=-1;i=E[i].next){ v=E[i].v; if(E[i].c-E[i].f&&dep[v]+1==dep[u]){ flag=true; cur[u]=pre[v]=i; break; } } if(flag){ u=v; continue; } int Min=N; for(int i=head[u];i!=-1;i=E[i].next) if(E[i].c-E[i].f&&dep[E[i].v]<Min){ Min=dep[E[i].v]; cur[u]=i; } gap[dep[u]]--; if(!gap[dep[u]])return ans; dep[u]=Min+1; gap[dep[u]]++; if(u!=start)u=E[pre[u]^1].v; } return ans; } char a[105][105],b[105][105],id[105][105]; int main(){ //freopen("E:\\read.txt","r",stdin); int n,m,d,cnt,liz,T; int x,y; int s,t; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ scanf("%d %d",&n,&d); Init(); cnt=liz=0; for(int i=1;i<=n;i++){ scanf("%s",a[i]+1); for(int j=1;a[i][j];j++){ if(a[i][j]>'0'){ cnt++; id[i][j]=cnt; } } } m=strlen(a[1]+1); for(int i=1;i<=n;i++){ scanf("%s",b[i]+1); for(int j=1;b[i][j];j++){ if(b[i][j]=='L'){ liz++; if(a[i][j]=='0') id[i][j]=++cnt; } } } s=2*cnt+1; t=s+1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ int ID1=id[i][j]; if(i-d<1||i+d>n||j-d<1||j+d>m) if(a[i][j]>'0') add_edge(ID1*2,t,oo); if(b[i][j]=='L'){ add_edge(s,ID1*2-1,oo); } if(a[i][j]>'0'){ add_edge(ID1*2-1,ID1*2,a[i][j]-'0'); } if(a[i][j]>'0') for(int p=-d;p<=d;p++) for(int q=-d;q<=d;q++){ if(abs(q)+abs(p)<=0||abs(q)+abs(p)>d)continue; x=i+p; y=j+q; if(a[x][y]=='0')continue; if(x>=1&&x<=n&&y>=1&&y<=m){ int ID2=id[x][y]; add_edge(ID1*2,ID2*2-1,oo); } } } } int ans=liz-isap(s,t,t); if(ans>1) printf("Case #%d: %d lizards were left behind.\n",cas,ans); else if(ans==1) printf("Case #%d: %d lizard were left behind.\n",cas,ans); else printf("Case #%d: no lizard was left behind.\n",cas); } return 0; } /* 1 5 3 11111 11111 11011 11111 11111 ..... ..... ..L.. ..... ..... */