标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
//============================================================================ // Name : // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; //**************************************************** //最大流模板 SAP算法 //邻接表形式 //****************************************************** const int MAXN=2200; const int MAXE=200020; const int INF=0x3f3f3f3f; struct Node { int to,next,cap; }edge[MAXE]; int tol; int head[MAXN]; int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN]; void init() { tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int w,int rw=0) { edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++; edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];head[v]=tol++; } int SAP(int start,int end,int nodenum) { memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); memcpy(cur,head,sizeof(head)); int u=pre[start]=start,maxflow=0,aug=-1; gap[0]=nodenum; while(dis[start]<nodenum) { loop: for(int &i=cur[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap&&dis[u]==dis[v]+1) { if(aug==-1||aug>edge[i].cap) aug=edge[i].cap; pre[v]=u; u=v; if(v==end) { maxflow+=aug; for(u=pre[u];v!=start;v=u,u=pre[u]) { edge[cur[u]].cap-=aug; edge[cur[u]^1].cap+=aug; } aug=-1; } goto loop; } } int mindis=nodenum; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap&&mindis>dis[v]) { cur[u]=i; mindis=dis[v]; } } if((--gap[dis[u]])==0)break; gap[dis[u]=mindis+1]++; u=pre[u]; } return maxflow; } char m1[30][30]; char m2[30][30]; int mat[30][30]; int main() { int T; int n,d; int iCase=0; scanf("%d",&T); while(T--){ iCase++; scanf("%d%d",&n,&d); init(); int tol=0; for(int i=0;i<n;i++) scanf("%s",m1[i]); int m=strlen(m1[0]); memset(mat,0,sizeof(mat)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(m1[i][j]>‘0‘){ mat[i][j]=++tol; addedge(2*tol-1,2*tol,m1[i][j]-‘0‘); } } } int st=0,ed=2*tol+1,nodenum=2*tol+2; int sum=0; for(int i=0;i<n;i++){ scanf("%s",m2[i]); for(int j=0;j<m;j++){ if(m2[i][j]==‘L‘){ sum++; addedge(st,2*mat[i][j]-1,1); } } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(mat[i][j]){ for(int x=-d;x<=d;x++){ for(int y=abs(x)-d;y<=d-abs(x);y++){ int mx=i+x; int my=j+y; if(mx<0||mx>=n||my<0||my>=m) continue; if(mat[mx][my]==0) continue; if(mx==i&&my==j) continue; addedge(2*mat[i][j],2*mat[mx][my]-1,INF); } } if(i<d||j<d||n-i<=d||m-j<=d) addedge(2*mat[i][j],ed,INF); } } } int ans=sum-SAP(st,ed,nodenum); if(ans==0) printf("Case #%d: no lizard was left behind.\n",iCase); else if(ans==1) printf("Case #%d: 1 lizard was left behind.\n",iCase); else printf("Case #%d: %d lizards were left behind.\n",iCase,ans); } return 0; }
HDU 2732 Leapin' Lizards (最大流)
标签:
原文地址:http://www.cnblogs.com/wangdongkai/p/5620719.html