POJ2711 Leapin' Lizards(最大流)

比较形象的是地图每个点都拆成三个点,这三个点限制流量为0或1,于是再一分为二,这样每个点都被拆成6个点。。。

其实拆两个点,连容量为柱子高的边,这样就行了。。

这题我掉坑了,“1 lizard was left behind.”。。虽然样例都把一切都说了。。要注意细节。。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<queue>
  4 #include<algorithm>
  5 using namespace std;
  6 #define INF (1<<30)
  7 #define MAXN 888 
  8 #define MAXM 888*888*2 
  9 
 10 struct Edge{
 11     int v,cap,flow,next;
 12 }edge[MAXM];
 13 int vs,vt,NE,NV;
 14 int head[MAXN];
 15 
 16 void addEdge(int u,int v,int cap){
 17     edge[NE].v=v; edge[NE].cap=cap; edge[NE].flow=0;
 18     edge[NE].next=head[u]; head[u]=NE++;
 19     edge[NE].v=u; edge[NE].cap=0; edge[NE].flow=0;
 20     edge[NE].next=head[v]; head[v]=NE++;
 21 }
 22 
 23 int level[MAXN];
 24 int gap[MAXN];
 25 void bfs(){
 26     memset(level,-1,sizeof(level));
 27     memset(gap,0,sizeof(gap));
 28     level[vt]=0;
 29     gap[level[vt]]++;
 30     queue<int> que;
 31     que.push(vt);
 32     while(!que.empty()){
 33         int u=que.front(); que.pop();
 34         for(int i=head[u]; i!=-1; i=edge[i].next){
 35             int v=edge[i].v;
 36             if(level[v]!=-1) continue;
 37             level[v]=level[u]+1;
 38             gap[level[v]]++;
 39             que.push(v);
 40         }
 41     }
 42 }
 43 
 44 int pre[MAXN];
 45 int cur[MAXN];
 46 int ISAP(){
 47     bfs();
 48     memset(pre,-1,sizeof(pre));
 49     memcpy(cur,head,sizeof(head));
 50     int u=pre[vs]=vs,flow=0,aug=INF;
 51     gap[0]=NV;
 52     while(level[vs]<NV){
 53         bool flag=false;
 54         for(int &i=cur[u]; i!=-1; i=edge[i].next){
 55             int v=edge[i].v;
 56             if(edge[i].cap!=edge[i].flow && level[u]==level[v]+1){
 57                 flag=true;
 58                 pre[v]=u;
 59                 u=v;
 60                 //aug=(aug==-1?edge[i].cap:min(aug,edge[i].cap));
 61                 aug=min(aug,edge[i].cap-edge[i].flow);
 62                 if(v==vt){
 63                     flow+=aug;
 64                     for(u=pre[v]; v!=vs; v=u,u=pre[u]){
 65                         edge[cur[u]].flow+=aug;
 66                         edge[cur[u]^1].flow-=aug;
 67                     }
 68                     //aug=-1;
 69                     aug=INF;
 70                 }
 71                 break;
 72             }
 73         }
 74         if(flag) continue;
 75         int minlevel=NV;
 76         for(int i=head[u]; i!=-1; i=edge[i].next){
 77             int v=edge[i].v;
 78             if(edge[i].cap!=edge[i].flow && level[v]<minlevel){
 79                 minlevel=level[v];
 80                 cur[u]=i;
 81             }
 82         }
 83         if(--gap[level[u]]==0) break;
 84         level[u]=minlevel+1;
 85         gap[level[u]]++;
 86         u=pre[u];
 87     }
 88     return flow;
 89 }
 90 char map1[22][22],map2[22][22];
 91 int main(){
 92     int t,n,d;
 93     scanf("%d",&t);
 94     for(int cse=1; cse<=t; ++cse){
 95         scanf("%d%d",&n,&d);
 96         for(int i=0; i<n; ++i) scanf("%s",map1[i]);
 97         for(int i=0; i<n; ++i) scanf("%s",map2[i]);
 98 
 99         int tot=0,m=strlen(map1[0]);
100         vs=n*m*2; vt=vs+1; NV=vt+1; NE=0;
101         memset(head,-1,sizeof(head));
102 
103         for(int i=0; i<n; ++i){
104             for(int j=0; j<m; ++j){
105                 addEdge(i*m+j,i*m+j+n*m,map1[i][j]-'0');
106                 for(int ni=0; ni<n; ++ni){
107                     for(int nj=0; nj<m; ++nj){
108                         if(ni==i && nj==j) continue;
109                         if((nj-j)*(nj-j)+(ni-i)*(ni-i)<=d*d) addEdge(i*m+j+n*m,ni*m+nj,INF);
110                     }
111                 }
112                 if(map2[i][j]=='L') addEdge(vs,i*m+j,1),++tot;
113                 if(i+1<=d||n-i<=d || j+1<=d||m-j<=d) addEdge(i*m+j+n*m,vt,INF);
114             }
115         }
116         tot-=ISAP();
117         if(tot==0) printf("Case #%d: no lizard was left behind.\n",cse);
118         else if(tot==1) printf("Case #%d: 1 lizard was left behind.\n",cse);
119         else printf("Case #%d: %d lizards were left behind.\n",cse,tot);
120     }
121     return 0;
122 }

 

posted @ 2015-10-03 11:17  WABoss  阅读(243)  评论(0编辑  收藏  举报