首页 > 代码库 > HDU 3046 Pleasant sheep and big big wolf
HDU 3046 Pleasant sheep and big big wolf
Pleasant sheep and big big wolf
Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 304664-bit integer IO format: %I64d Java class name: Main
In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an obligation to protect pleasant sheep and his companions to free from being disturbed by big big wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.
Input
There are many cases.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
Output
For every case:
First line output “Case p:”, p is the p-th case;
The second line is the answer.
First line output “Case p:”, p is the p-th case;
The second line is the answer.
Sample Input
4 61 0 0 1 0 00 1 1 0 0 02 0 0 0 0 00 2 0 1 1 0
Sample Output
Case 1:4
Source
2009 Multi-University Training Contest 14 - Host by ZJNU
解题:最小割。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 210*210;18 struct arc{19 int to,flow,next;20 arc(int x = 0,int y = 0,int z = -1){21 to = x;22 flow = y;23 next = z;24 }25 };26 arc e[maxn<<2];27 int head[maxn],d[maxn],cur[maxn];28 int tot,S,T,n,m;29 void add(int u,int v,int flow){30 e[tot] = arc(v,flow,head[u]);31 head[u] = tot++;32 e[tot] = arc(u,flow,head[v]);//可以不这么建图。。。。33 head[v] = tot++;34 }35 bool bfs(){36 memset(d,-1,sizeof(d));37 queue<int>q;38 q.push(T);39 d[T] = 1;40 while(!q.empty()){41 int u = q.front();42 q.pop();43 for(int i = head[u]; ~i; i = e[i].next){44 if(e[i^1].flow && d[e[i].to] == -1){45 d[e[i].to] = d[u] + 1;46 q.push(e[i].to);47 }48 }49 }50 return d[S] > -1;51 }52 int dfs(int u,int low){53 if(u == T) return low;54 int tmp = 0,a;55 for(int &i = cur[u]; ~i; i = e[i].next){56 if(e[i].flow && d[e[i].to]+1==d[u]&&(a=dfs(e[i].to,min(low,e[i].flow)))){57 e[i].flow -= a;58 e[i^1].flow += a;59 low -= a;60 tmp += a;61 if(!low) break;62 }63 }64 if(!tmp) d[u] = -1;65 return tmp;66 }67 int dinic(){68 int ans = 0;69 while(bfs()){70 memcpy(cur,head,sizeof(head));71 ans += dfs(S,INF);72 }73 return ans;74 }75 int main() {76 int u,v,w,cs = 1;77 while(~scanf("%d %d",&n,&m)){78 memset(head,-1,sizeof(head));79 S = n*m;80 T = n*m+1;81 for(int i = tot = 0; i < n; ++i)82 for(int j = 0; j < m; ++j){83 scanf("%d",&u);84 if(i) add(m*(i-1)+j,m*i+j,1);85 if(j) add(m*i+j-1,m*i+j,1);86 if(u == 1) add(S,m*i+j,INF);87 if(u == 2) add(m*i+j,T,INF);88 }89 printf("Case %d:\n%d\n",cs++,dinic());90 }91 return 0;92 }
HDU 3046 Pleasant sheep and big big wolf
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。