[BZOJ2127] hapiness

happiness

Time Limit: 51 Sec  Memory Limit: 259 MB

1 2
1 1
100 110
1
1000

1210
【样例说明】

【数据规模】

SOLUTION

  1 #include<cmath>
2 #include<queue>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<cstring>
6 #include<iostream>
7 #include<algorithm>
8 #define INF 1000000
9 using namespace std;
10
11 int zhao;
12 int n,m,peo;
13 int a[30][30];
14 int dor[30];
15 int id[30][30];
16 int dis[410][410];
17 int mov[6][3];
18 int jia;
19 int S,T;
20
21 struct node{
22     int u,v,w,nxt;
23 }g[5000100];
24 int adj[5000100],e;
25 void add(int u,int v,int w){
26     g[e].v=v; g[e].u=u; g[e].w=w;
27     g[e].nxt=adj[u]; adj[u]=e++;
28 }
29 bool Jud(int x,int y,int i,int pos){
30     int x1=x+mov[i][0] ,y1=y+mov[i][1];
31     if(!(x1>=1 && x1<=n && y1>=1 && y1<=m)) return 0;
32     if(a[x1][y1]==0 || a[x1][y1]==2) return 0;
33     int pos2=(x+mov[i][0]-1)*m+ ( y+mov[i][1] );
34     if(dis[pos2][pos]<10000) return 0;
35     return 1;
36 }
37 void find_short(int pos){
38     queue<int> q;
39     q.push(pos);
40     dis[pos][pos]=0;
41     int k;
42     int x,y;
43     while(!q.empty()){
44         k=q.front(); q.pop();
45         //cout<<"k== "<<k<<endl;
46         x=(k-1)/m+1; y=k%m; if(y==0) y=m;
47         for(int i=1;i<=4;i++){
48             if(Jud(x,y,i,pos)){
49                 int pos2=(x+mov[i][0]-1)*m+ ( y+mov[i][1] );
50                 dis[pos2][pos]=dis[k][pos]+1;
51                 //cout<<"pos== "<<pos2<<"  "<<pos<<"  "<<dis[pos2][pos]<<endl;
52                 q.push(pos2);
53             }
54         }
55     }
56 }
57 void init(){
58     scanf("%d%d",&n,&m);
59     jia=n*n;
60     char s[30];
61     for(int i=1;i<=n;i++){
62         scanf("%s",&s);
63         for(int j=0;j<m;j++){
64             if(s[j]=='.')      a[i][j+1]=1,peo++;
65             else if(s[j]=='X') a[i][j+1]=0;
66             else if(s[j]=='D') a[i][j+1]=2;
67         }
68     }
69     for(int i=1;i<=n;i++)
70         for(int j=1;j<=m;j++){
71             id[i][j]=m*(i-1)+j;
72             if(a[i][j]==2) dor[++dor[0]]=id[i][j];
73         }
74     mov[1][0]=0; mov[1][1]=1;
75
76     mov[2][0]=0; mov[2][1]=-1;
77     mov[3][0]=1; mov[3][1]=0;
78     mov[4][0]=-1;mov[4][1]=0;
79     memset(dis,30,sizeof(dis));
80     int x,y;
81     for(int i=1;i<=dor[0];i++){
82         find_short(dor[i]);
83     }
84 }
85 bool nengpao(){
86     for(int i=1;i<=n;i++){
87         for(int j=1;j<=m;j++){
88             bool ok=0;
89             if(a[i][j]==0 || a[i][j]==2) continue;
90             for(int k=1;k<=dor[0];k++){
91                 if(dis[id[i][j]][dor[k]]<10000) ok=1;
92             }
93             if(!ok) return 0;
94         }
95     }
96     return 1;
97 }
98 int dep[300010];
99 bool BFS(){
100     //if(zhao==149) cout<<zhao<<endl;
101     memset(dep,0,sizeof(dep));
102     queue<int> q; int k;
103     dep[S]=1;
104     q.push(S);
105     while(!q.empty()){
106         k=q.front(); q.pop();
107         for(int i=adj[k];i!=-1;i=g[i].nxt){
108             int v=g[i].v;
109             if(g[i].w && !dep[v]){
110                 dep[v]=dep[k]+1;
111                 if(v==T) return 1;
112                 q.push(v);
113             }
114         }
115     }
116     return 0;
117 }
118 int dfs(int x,int fw){
119     //printf("x==%d   fw==%d\n",x,fw);
120     if(x==T) return fw;
121     int tmp=fw,k;
122     for(int i=adj[x];i!=-1;i=g[i].nxt){
123         int v=g[i].v;
124         if(g[i].w && tmp && dep[v]==dep[x]+1){
125             k=dfs(v,min(tmp,g[i].w));
126             if(!k){
127                 dep[v]=0;
128                 continue;
129             }
130             g[i].w-=k; g[i^1].w+=k; tmp-=k;
131         }
132     }
133     return fw-tmp;
134 }
135 bool check(int lim){
136     int ch=lim; zhao=lim;
137     memset(adj,-1,sizeof(adj)); e=0;
138     for(int i=1;i<=dor[0];i++){
139         for(int j=1;j<=lim;j++){
140             add((i-1)*ch+j+jia,T,1); add(T,(i-1)*ch+j+jia,0);
141         }
142     }
143     for(int i=1;i<=n;i++){
144         for(int j=1;j<=m;j++){
145             //cout<<"id2=="<<id[i][j]<<endl;
146             if(a[i][j]==0 || a[i][j]==2) continue;
147             //cout<<"id== "<<id[i][j]<<endl;
148             add(S,id[i][j],1); add(id[i][j],S,0);
149             for(int k=1;k<=dor[0];k++){
150                 if(dis[id[i][j]][dor[k]]>10000) continue;
151                 for(int hh=dis[id[i][j]][dor[k]];hh<=lim;hh++){
152                     add(id[i][j],(k-1)*ch+hh+jia,1), add((k-1)*ch+hh+jia,id[i][j],0);
153                     //if(lim==300) cout<<id[i][j]<<"   "<<(k-1)*ch+hh+jia<<endl;
154                 }
155             }
156         }
157     }
158     int he=0,tan;
159     while(BFS()){
160         while(tan=dfs(S,INF)){ he+=tan; }
161     }
162     //cout<<"lim== "<<lim<<"  "<<he<<endl;
163     if(he>=peo) return 1;
164     else return 0;
165
166 }
167 void work(){
168     S=0; T=300000;
169     int l=0,r=600,mid,ans=600;
170     while(l<=r){
171         mid=(l+r)>>1;
172         if(check(mid)) ans=mid,r=mid-1;
173         else l=mid+1;
174     }
175     printf("%d\n",ans);
176     return;
177 }
178 int main(){
179     //freopen("a.in","r",stdin);
180     //freopen("a.out","w",stdout);
181     init();
182     bool ok=nengpao();
183     if(!ok){
184         printf("impossible\n");
185         return 0;
186     }
187     work();
188     return 0;
189
190 }

posted @ 2017-08-01 06:36  Nawox  阅读(236)  评论(0编辑  收藏  举报