hdu 2732 Leapin\' Lizards (最大流 拆点建图)
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety… but there\’s a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.
always 1 ≤ d ≤ 3.
建图:
源点S编号0,网格的每个格子分成两个点i和i+n*m(n和m为网格的行和列数,其实i编号点是表示蜥蜴进来,而i+n*m编号的点是表示蜥蜴出去).汇点t编号n*m*2+1.
如果格子i上有蜥蜴,那么从s到i有边(s,i,1).
如果格子i能承受x次跳出,那么有边(i,i+n*m,x)
如果从格子i能直接跳出网格边界,那么有边(i+n*m,t,INF)
如果从格子i不能直接跳出网格,那么从i到离i距离<=d的网格j有边(i+n*m,j,INF). 注意这里的距离是abs(行号之差)+abs(列号之差)
最终我们求出的最大流就是能跳出网格的蜥蜴数.
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const int maxn = 1000; 5 const int inf = 0x3f3f3f3f; 6 char s[100][100]; 7 char ss[100][100]; 8 struct Edge 9 { 10 int from,to,cap,flow; 11 Edge (){} 12 Edge (int f,int t,int c,int fl){from=f,to=t,cap=c,flow=fl;} 13 }; 14 struct Dinic 15 { 16 int n,m,s,t; 17 vector <Edge> edges; 18 vector <int> G[maxn]; 19 int cur[maxn]; 20 int dep[maxn]; 21 bool vis[maxn]; 22 void init (int n,int s,int t) 23 { 24 this->n=n;this->s=s;this->t=t; 25 edges.clear(); 26 for (int i=0;i<n;++i) 27 G[i].clear(); 28 } 29 void addedge (int from,int to,int cap) 30 { 31 edges.push_back(Edge(from,to,cap,0)); 32 edges.push_back(Edge(to,from,0,0)); 33 m = edges.size(); 34 G[from].push_back(m-2); 35 G[to].push_back(m-1); 36 } 37 bool bfs () 38 { 39 queue <int> q; 40 while (!q.empty()) q.pop(); 41 memset(vis,false,sizeof vis); 42 vis[s] = true; 43 dep[s] = 0; 44 q.push(s); 45 while (!q.empty()){ 46 int u = q.front(); 47 q.pop(); 48 for (int i=0;i<G[u].size();++i){ 49 Edge e = edges[G[u][i]]; 50 int v = e.to; 51 if(!vis[v]&&e.cap>e.flow){ 52 vis[v] = true; 53 dep[v] = dep[u] + 1; 54 q.push(v); 55 } 56 } 57 } 58 return vis[t]; 59 } 60 int dfs (int x,int mi){ 61 if (x==t||mi==0) return mi; 62 int flow = 0,f; 63 for (int &i=cur[x];i<G[x].size();++i){ 64 Edge &e = edges[G[x][i]]; 65 int y = e.to; 66 if (dep[y]==dep[x]+1&&(f=dfs(y,min(mi,e.cap-e.flow)))>0){ 67 e.flow+=f; 68 edges[G[x][i]^1].flow-=f; 69 flow+=f; 70 mi-=f; 71 if (mi==0) break; 72 } 73 } 74 return flow; 75 } 76 int max_flow () 77 { 78 int ans = 0; 79 while (bfs()){ 80 memset(cur,0,sizeof cur); 81 ans+=dfs(s,inf); 82 } 83 return ans; 84 } 85 }dinic; 86 int full_flow; 87 bool check (int x,int y,int i,int j,int d) 88 { 89 if (abs(x-i)+abs(y-j)<=d) return true; 90 else return false; 91 } 92 bool out (int x,int y,int d) 93 { 94 if (x-d) return true; 95 else return false; 96 } 97 int main() 98 { 99 //freopen("de.txt","r",stdin); 100 int casee = 0; 101 int T; 102 scanf("%d",&T); 103 while (T--){ 104 int n,m,src,dst,d; 105 full_flow = 0; 106 scanf("%d%d",&n,&d); 107 for (int i=1;i<=n;++i) 108 scanf("%s",s[i]+1); 109 int len = strlen(s[1]+1); 110 m = len; 111 src = 0; dst = 2*n*m+1; 112 dinic.init(2*n*m+2,src,dst); 113 for (int i=1;i<=n;++i){ 114 for (int j=1;j<=len;++j){ 115 if (s[i][j]-\'0\'>0){ 116 int id = (i-1)*m+j; 117 dinic.addedge(id,id+n*m,s[i][j]-\'0\'); 118 if (i<=d || i+d>n || j<=d || j+d>m){//这个点能直接跳出去 119 dinic.addedge(id+n*m,dst,inf); 120 } 121 else{ 122 for (int x=1;x<=n;++x){ 123 for (int y=1;y<=m;++y){ 124 if (x==i&&y==j) continue; 125 if (check(x,y,i,j,d)){ 126 int id2 = (x-1)*m+y; 127 dinic.addedge(id+n*m,id2,inf);//这个点的出连向能到达点的入 128 //dinic.addedge(id2+n*m,id,inf); 129 } 130 } 131 } 132 } 133 } 134 135 } 136 } 137 for (int i=1;i<=n;++i){ 138 scanf("%s",ss[i]+1); 139 for (int j=1;j<=len;++j){ 140 if (ss[i][j]==\'L\'){ 141 full_flow++; 142 int id = (i-1)*m+j; 143 dinic.addedge(src,id,1); 144 } 145 } 146 } 147 int ans = full_flow-dinic.max_flow(); 148 if(ans==0) printf("Case #%d: no lizard was left behind.\n",++casee); 149 else if(ans==1) printf("Case #%d: 1 lizard was left behind.\n",++casee); 150 else printf("Case #%d: %d lizards were left behind.\n",++casee,ans); 151 } 152 return 0; 153 }