bzoj1066[SCOI2007]蜥蜴

bzoj1066[SCOI2007]蜥蜴

题意:

r行c列网格图上有一些高低不平的柱子,一些柱子上有蜥蜴,一只蜥蜴一次能跳距离为d,每次蜥蜴跳跃时出发柱子高度减一,当柱子高度为0时消失,问最少多少蜥蜴不能跳出网格图。r,c≤20,d≤4

题解:

裸最大流,每个柱子拆成X,Y两点,两点之间连柱子的高度,所有Yi向可达柱子的Xi连边,s向所有蜥蜴初始位置连边,所有可以跳出图的柱子向t连边。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define maxn 1000
 6 #define inc(i,j,k) for(int i=j;i<=k;i++)
 7 #define INF 0x3fffffff
 8 using namespace std;
 9 
10 struct e{int t,c,n;}; e es[maxn*40]; int g[maxn],ess;
11 inline void pe(int f,int t,int c){
12     es[++ess]=(e){t,c,g[f]}; g[f]=ess; es[++ess]=(e){f,0,g[t]}; g[t]=ess;
13 }
14 inline void init(){
15     ess=-1; memset(g,-1,sizeof(g));
16 }
17 queue <int> q; int h[maxn];
18 bool bfs(int s,int t){
19     memset(h,-1,sizeof(h)); while(!q.empty())q.pop(); h[s]=0; q.push(s);
20     while(! q.empty()){
21         int x=q.front(); q.pop();
22         for(int i=g[x];i!=-1;i=es[i].n)if(es[i].c&&h[es[i].t]==-1)h[es[i].t]=h[x]+1,q.push(es[i].t);
23     }
24     return h[t]!=-1;
25 }
26 int dfs(int x,int t,int f){
27     if(x==t)return f; int u=0;
28     for(int i=g[x];i!=-1;i=es[i].n)if(es[i].c&&h[es[i].t]==h[x]+1){
29         int w=dfs(es[i].t,t,min(f,es[i].c)); f-=w; u+=w; es[i].c-=w; es[i^1].c+=w; if(f==0)return u;
30     }
31     if(u==0)h[x]=-1; return u;
32 }
33 int dinic(int s,int t){
34     int f=0; while(bfs(s,t))f+=dfs(s,t,INF); return f;
35 }
36 inline int dis(int x1,int y1,int x2,int y2){
37     return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
38 }
39 int r,c,d,s,t,pos[30][30],x[500],y[500],he[500],n,tot; char str[30];
40 int main(){
41     scanf("%d%d%d",&r,&c,&d); n=tot=0;
42     inc(i,1,r){
43         scanf("%s",str);
44         inc(j,0,c-1)if(str[j]!='0')
45             n++,x[n]=i,y[n]=j+1,he[n]=str[j]-'0',pos[i][j+1]=n;
46     }
47     init(); s=0; t=2*n+1;
48     inc(i,1,r){
49         scanf("%s",str);
50         inc(j,0,c-1)if(str[j]=='L')pe(s,pos[i][j+1],1),tot++;
51     }
52     inc(i,1,n){
53         pe(i,n+i,he[i]);
54         inc(j,1,n)if(i!=j&&dis(x[i],y[i],x[j],y[j])<=d*d)pe(n+i,j,INF);
55         if(x[i]<=d||r+1-x[i]<=d||y[i]<=d||c+1-y[i]<=d)pe(n+i,t,INF);
56     }
57     printf("%d",tot-dinic(s,t)); return 0;
58 }

 

20160608

原文地址:https://www.cnblogs.com/YuanZiming/p/5778165.html