标签:
1 #include<cstdio> 2 #include<iostream> 3 #include<string> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 #include<algorithm> 8 using namespace std; 9 10 struct Edge 11 { 12 int from,to,cap,flow; 13 }; 14 const int maxn=1005; 15 const int inf=0x3f3f3f; 16 17 struct dinic 18 { 19 int n,m,s,t; 20 vector<Edge>edges; 21 vector<int>G[maxn]; 22 bool vis[maxn]; 23 int d[maxn]; 24 int cur[maxn]; 25 26 void init(int num) 27 { 28 for(int i=0;i<num;i++)G[i].clear(); 29 edges.clear(); 30 } 31 32 void addedge(int from,int to,int cap) 33 { 34 edges.push_back((Edge){from,to,cap,0}); 35 edges.push_back((Edge){to,from,0,0}); 36 int m=edges.size(); 37 G[from].push_back(m-2); 38 G[to].push_back(m-1); 39 } 40 41 bool bfs() 42 { 43 memset(vis,0,sizeof(vis)); 44 queue<int>q; 45 q.push(s); 46 d[s]=0; 47 vis[s]=1; 48 while(!q.empty()) 49 { 50 int x=q.front();q.pop(); 51 for(int i=0;i<G[x].size();i++) 52 { 53 Edge& e=edges[G[x][i]]; 54 if(!vis[e.to]&&e.cap>e.flow) 55 { 56 vis[e.to]=1; 57 d[e.to]=d[x]+1; 58 q.push(e.to); 59 } 60 } 61 } 62 return vis[t]; 63 } 64 65 int dfs(int x,int a) 66 { 67 if(x==t||a==0)return a; 68 int flow=0,f; 69 for(int& i=cur[x];i<G[x].size();i++) 70 { 71 Edge& e=edges[G[x][i]]; 72 if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0) 73 { 74 e.flow+=f; 75 edges[G[x][i]^1].flow-=f; 76 flow+=f; 77 a-=f; 78 if(a==0)break; 79 } 80 } 81 return flow; 82 } 83 84 int maxflow(int s,int t) 85 { 86 this->s=s,this->t=t; 87 int flow=0; 88 while(bfs()) 89 { 90 memset(cur,0,sizeof(cur)); 91 flow+=dfs(s,inf); 92 } 93 return flow; 94 } 95 }dc; 96 97 int main() 98 { 99 int T; 100 scanf("%d",&T); 101 for(int kase=1;kase<=T;kase++) 102 { 103 int m,n,d,sum=0; 104 scanf("%d%d",&n,&d); 105 for(int i=0;i<n;i++) 106 { 107 string s; 108 cin>>s; 109 if(i==0)m=s.size(),dc.init(2*n*m+2); 110 for(int j=0;j<m;j++) 111 { 112 int id=i*m+j+1; 113 if(s[j]-‘0‘>0) 114 { 115 dc.addedge(id,id+m*n,s[j]-‘0‘); 116 if(i<d||i+d>=n||j<d||j+d>=m) 117 dc.addedge(id+n*m,2*n*m+1,inf); 118 else 119 { 120 for(int k=0;k<n;k++) 121 for(int l=0;l<m;l++) 122 { 123 int id2=k*m+l+1; 124 if(id==id2)continue; 125 if(abs(i-k)+abs(j-l)<=d) 126 dc.addedge(id+n*m,id2,inf); 127 } 128 } 129 } 130 } 131 } 132 for(int i=0;i<n;i++) 133 { 134 string s; 135 cin>>s; 136 for(int j=0;j<s.size();j++) 137 if(s[j]==‘L‘) 138 { 139 sum++; 140 dc.addedge(0,i*m+j+1,1); 141 } 142 } 143 int ans=sum-dc.maxflow(0,2*m*n+1); 144 if(ans==0) printf("Case #%d: no lizard was left behind.\n",kase); 145 else if(ans==1) printf("Case #%d: 1 lizard was left behind.\n",kase); 146 else printf("Case #%d: %d lizards were left behind.\n",kase,ans); 147 } 148 return 0; 149 }
标签:
原文地址:http://www.cnblogs.com/homura/p/4907501.html