poj 2711 Leapin' Lizards && BZOJ 1066: [SCOI2007]蜥蜴 最大流

题目链接:http://poj.org/problem?id=2711

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1066

Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room's floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below... 

Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties. 

The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.

 

题意描述:在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外。 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一个石柱上。石柱都不稳定,每次当蜥蜴跳跃时,所离开的石柱高度减1(如果仍然落在地图内部,则到达的石柱高度不变),如果该石柱原来高度为1,则蜥蜴离开后消失。以后其他蜥蜴不能落脚。任何时刻不能有两只蜥蜴在同一个石柱上。

算法分析:新增源点from和汇点to,每个位置u拆边u->r*c+u(如果位置上没石柱,权值为inf,否则权值为石柱的高度);石柱之间如果距离不超过D,连边并权值为无穷大;

from连边有蜥蜴的位置(点拆分后的前驱点,权值为1);如果点的位置距离边界外不超过D,则该点拆分后的后继点连边汇点to,权值为inf。最大流解之即可。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<queue>
  8 #define inf 0x7fffffff
  9 using namespace std;
 10 const int maxn=900+10;
 11 const int M = 700000+10;
 12 
 13 int r,c,from,to;
 14 int D;
 15 struct node
 16 {
 17     int v,flow;
 18     int next;
 19 }edge[M*4];
 20 int head[maxn],edgenum;
 21 
 22 void add(int u,int v,int flow)
 23 {
 24     edge[edgenum].v=v ;edge[edgenum].flow=flow ;
 25     edge[edgenum].next=head[u] ;head[u]=edgenum++ ;
 26 
 27     edge[edgenum].v=u ;edge[edgenum].flow=0 ;
 28     edge[edgenum].next=head[v] ;head[v]=edgenum++ ;
 29 }
 30 
 31 int d[maxn];
 32 int bfs()
 33 {
 34     memset(d,0,sizeof(d));
 35     d[from]=1;
 36     queue<int> Q;
 37     Q.push(from);
 38     while (!Q.empty())
 39     {
 40         int u=Q.front() ;Q.pop() ;
 41         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 42         {
 43             int v=edge[i].v;
 44             if (!d[v] && edge[i].flow)
 45             {
 46                 d[v]=d[u]+1;
 47                 Q.push(v);
 48                 if (v==to) return 1;
 49             }
 50         }
 51     }
 52     return 0;
 53 }
 54 
 55 int dfs(int u,int flow)
 56 {
 57     if (u==to || flow==0) return flow;
 58     int cap=flow;
 59     for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 60     {
 61         int v=edge[i].v;
 62         if (d[v]==d[u]+1 && edge[i].flow)
 63         {
 64             int x=dfs(v,min(edge[i].flow,cap));
 65             edge[i].flow -= x;
 66             edge[i^1].flow += x;
 67             cap -= x;
 68             if (cap==0) return flow;
 69         }
 70     }
 71     return flow-cap;
 72 }
 73 
 74 int dinic()
 75 {
 76     int sum=0;
 77     while (bfs()) sum += dfs(from,inf);
 78     return sum;
 79 }
 80 
 81 int main()
 82 {
 83     char str[22][22];
 84     char str2[22][22];
 85     int ncase=1;
 86     int t;
 87     scanf("%d",&t);
 88     while (t--)
 89     {
 90         memset(str,0,sizeof(str));
 91         scanf("%d%d",&r,&D);
 92         for (int i=1 ;i<=r ;i++)
 93             scanf("%s",str[i]+1);
 94         c=strlen(str[1]+1);
 95         memset(head,-1,sizeof(head));
 96         edgenum=0;
 97         from=2*r*c+1;
 98         to=from+1;
 99         for (int i=1 ;i<=r ;i++)
100         {
101             for (int j=1 ;j<=c ;j++)
102             if (str[i][j]!='0')
103             {
104                 for (int u=1 ;u<=r ;u++)
105                 {
106                     for (int v=1 ;v<=c ;v++)
107                     {
108                         if (u==i && v==j) continue;
109                         if (str[u][v]=='0') continue;
110                         int dis=abs(u-i)+abs(v-j);
111                         if (dis<=D) {
112                             add(r*c+(i-1)*c+j,(u-1)*c+v,inf);
113                         }
114                     }
115                 }
116             }
117         }
118         memset(str2,0,sizeof(str2));
119         for (int i=1 ;i<=r ;i++) scanf("%s",str2[i]+1);
120         int ans=0;
121         for (int i=1 ;i<=r ;i++)
122         {
123             for (int j=1 ;j<=c ;j++)
124             {
125                 if (str2[i][j]=='.') continue;
126                 ans ++ ;
127                 add(from,(i-1)*c+j,1);
128             }
129         }
130         for (int i=1 ;i<=r ;i++)
131         {
132             for (int j=1 ;j<=c ;j++)
133             {
134                 int num=str[i][j]-'0';
135                 add((i-1)*c+j,r*c+(i-1)*c+j,num==0 ? inf : num);
136                 if (i<=D || j<=D || (r-i)<=D-1 || (c-j)<=D-1)
137                     add(r*c+(i-1)*c+j,to,inf);
138             }
139         }
140         //cout<<ans<<endl;
141         int sum=ans-dinic();
142         printf("Case #%d: ",ncase++);
143         if (sum==0) printf("no lizard was left behind.\n");
144         else if (sum==1) printf("1 lizard was left behind.\n");
145         else printf("%d lizards were left behind.\n",sum);
146     }
147     return 0;
148 }

 

posted @ 2015-03-24 18:24  huangxf  阅读(518)  评论(0编辑  收藏