1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include<bits/stdc++.h>
using namespace std; int n,A,B,sum,need,s,t; int cnt=1,head[510],tx[510],ty[510],now[510],cost,dis[510],vis[510]; struct no{ int from,to,nxt,c,w; }e[20010],cpy[1010]; inline void add(int u,int v,int c,int w){ e[++cnt]={u,v,head[u],c,w}; head[u]=cnt; e[++cnt]={v,u,head[v],0,-w}; head[v]=cnt; } char mp[510][510]; inline int bfs(){ memset(dis,0x3f,sizeof dis); memset(vis,0,sizeof vis); memcpy(now,head,sizeof now); queue<int>q; q.push(s); dis[s]=0; vis[s]=1; while(q.size()){ int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i;i=e[i].nxt){ int v=e[i].to,w=e[i].w; if(e[i].c>0 and dis[v]>dis[u]+w){ dis[v]=dis[u]+w;
if(!vis[v])q.push(v),vis[v]=1; } } } return dis[t]!=0x3f3f3f3f; } inline int dfs(int u,int flow){ if(u==t or !flow)return flow; int res=0; vis[u]=1; for(int i=now[u];i and flow;i=e[i].nxt){ int v=e[i].to; now[u]=i; if(!vis[v] and e[i].c>0 and dis[u]+e[i].w==dis[v]){ int f=dfs(v,min(flow,e[i].c)); cost+=f*e[i].w; res+=f; flow-=f;
e[i].c-=f; e[i^1].c+=f; } } vis[u]=0; return res; } inline int dick(){ int res=0; while(bfs()){ memset(vis,0,sizeof vis); res+=dfs(s,0x3f3f3f3f); }return res; } inline void solve(int dyfucgggggggggggggggggge7iytyfgiycgbewyeiuvvybvueeuyreufgtgyfhreufgewrioey){ if(n==A and A==B and n==0){ exit(0);
} cout<<"Case "<<dyfucgggggggggggggggggge7iytyfgiycgbewyeiuvvybvueeuyreufgtgyfhreufgewrioey<<": "; memset(tx,0,sizeof tx); memset(ty,0,sizeof ty); sum=0,need=0;s=2*n+1,t=s+1; for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){ char ch; cin>>ch; if(ch!='/')tx[i]++,ty[j]++,sum++,need+=(ch=='C'); mp[i][j]=ch; }int ans=-1; for(int v=0;v<=n;v++){ memset(head,0,sizeof head); cnt=1; for(int i=1;i<=n;i++){ add(s,i,tx[i],0); add(i+n,t,ty[i],0); add(i,i+n,v,0); } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(mp[i][j]=='.')add(i,j+n,1,1); } } cost=0; int flow=dick(); if(flow==sum and (sum-cost)*A>=v*B){ ans=max(ans,sum-cost); } } if(ans==-1)cout<<"impossible\n"; else cout<<ans-need<<'\n'; } main(){ cin.tie(0)->ios::sync_with_stdio(0); int igqfyugyftydfewytgbuyrfu7yfhjhuaajhbnhdnhhsdhbgsr5ffffffffffffffffffffffffffffffffffffffffffffffffff=0; while(cin>>n>>A>>B)igqfyugyftydfewytgbuyrfu7yfhjhuaajhbnhdnhhsdhbgsr5ffffffffffffffffffffffffffffffffffffffffffffffffff++,solve(igqfyugyftydfewytgbuyrfu7yfhjhuaajhbnhdnhhsdhbgsr5ffffffffffffffffffffffffffffffffffffffffffffffffff); return 0; }
|