码迷,mamicode.com
首页 > 其他好文 > 详细

【BZOJ 1066】[SCOI2007]蜥蜴

时间:2016-02-13 21:53:03      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

Description

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

Input

输入第一行为三个整数r,c,d,即地图的规模与最大跳跃距离。以下r行为石竹的初始状态,0表示没有石柱,1~3表示石柱的初始高度。以下r行为蜥蜴位置,“L”表示蜥蜴,“.”表示没有蜥蜴。

Output

输出仅一行,包含一个整数,即无法逃离的蜥蜴总数的最小值。

Sample Input

5 8 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........

Sample Output

1

HINT

 

100%的数据满足:1<=r, c<=20, 1<=d<=4

这大概就是我做过的最难得网络流题了(没错,我就是这么弱,你打我啊)

拆点x,x‘,流量为高度,有蜥蜴的点x与S连边,容量为1,所有可以滚粗的点x‘与T连边,容量inf,所以可以互相到达的点x’,y连边,容量inf,跑最大流

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cmath>
  4 #include<iostream>
  5 using namespace std;
  6 const int inf=10000000,N=1010;
  7 struct ee{int to,next,f;}e[50010];
  8 int head[N],q[N*2],map[30][30],dis[N];
  9 int S,T,r,c,d,cnt=1,ans;
 10 char s[30];
 11 int sqr(int x) {return x*x;}
 12  
 13 int wz(int x,int y,int add){
 14     return ((x-1)*c+y+r*c*add);
 15 }
 16  
 17 double dist(int x1,int y1,int x2,int y2){
 18     return sqrt(sqr(x1-x2)+sqr(y1-y2));
 19 }
 20  
 21 void ins(int u,int v,int f){
 22     e[++cnt].to=v;e[cnt].f=f;e[cnt].next=head[u];head[u]=cnt;
 23     e[++cnt].to=u;e[cnt].f=0;e[cnt].next=head[v];head[v]=cnt;
 24 }
 25  
 26 int pd(int x,int y){
 27     if (x<=d||r-x<d||y<=d||c-y<d) return 1;
 28     return 0;
 29 }
 30  
 31 bool bfs(){
 32     for (int i=1;i<=T;i++) dis[i]=inf;
 33     int h=0,t=1,now;
 34     q[1]=S;dis[S]=0;
 35     while(h!=t){
 36         now=q[++h];
 37         for (int i=head[now];i;i=e[i].next){
 38             int v=e[i].to;
 39             if (e[i].f&&dis[now]+1<dis[v]){
 40                 dis[v]=dis[now]+1;
 41                 if (v==T)return 1;
 42                 q[++t]=v;
 43             }
 44         }
 45     }
 46     if (dis[T]==inf) return 0; return 1;
 47 }
 48  
 49 int dinic(int now,int f){
 50     if (now==T) return f;
 51     int rest=f;
 52     for (int i=head[now];i;i=e[i].next){
 53         int v=e[i].to;
 54         if (e[i].f&&dis[v]==dis[now]+1){
 55             int t=dinic(v,min(rest,e[i].f));
 56             if (!t) dis[v]=0;
 57             e[i].f-=t;
 58             e[i^1].f+=t;
 59             rest-=t;
 60         }
 61     }
 62     return f-rest;
 63 }
 64  
 65 int main(){
 66     scanf("%d%d%d",&r,&c,&d);
 67     S=0,T=2*r*c+1;
 68     for (int i=1;i<=r;i++){
 69         scanf("%s",s);
 70         int l=strlen(s);
 71         for (int j=0;j<l;j++) map[i][j+1]=s[j]-0;
 72     }
 73     for (int i=1;i<=r;i++){
 74         scanf("%s",s);
 75         int l=strlen(s);
 76         for (int j=0;j<l;j++){
 77             if (s[j]==L) ins(S,wz(i,j+1,0),1),ans++;
 78         }
 79     }
 80     for (int i=1;i<=r;i++)
 81         for (int j=1;j<=c;j++)
 82             if (map[i][j]) ins(wz(i,j,0),wz(i,j,1),map[i][j]);
 83      
 84     for (int x1=1;x1<=r;x1++)
 85         for (int y1=1;y1<=c;y1++){
 86             if (!map[x1][y1]) continue;
 87             for (int x2=1;x2<=r;x2++)
 88                 for (int y2=1;y2<=c;y2++){
 89                     if (x1==x2&&y1==y2) continue; 
 90                     if (map[x1][y1]&&map[x2][y2]&&dist(x1,y1,x2,y2)<=d){
 91                         ins(wz(x1,y1,1),wz(x2,y2,0),inf);
 92                         ins(wz(x2,y2,1),wz(x1,y1,0),inf);
 93                     }
 94                 }
 95             }
 96      
 97     for (int i=1;i<=r;i++)
 98         for (int j=1;j<=c;j++)
 99             if (pd(i,j)) ins(wz(i,j,1),T,inf); 
100     while(bfs()) 
101     ans-=dinic(S,inf);
102     printf("%d",ans);
103 }

 

【BZOJ 1066】[SCOI2007]蜥蜴

标签:

原文地址:http://www.cnblogs.com/wuminyan/p/5188311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!