您的位置:首页 > 其它

HDU 3046 Pleasant sheep and big big wolf

2014-11-13 19:28 302 查看

Pleasant sheep and big big wolf

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3046
64-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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 210*210;
struct arc{
int to,flow,next;
arc(int x = 0,int y = 0,int z = -1){
to = x;
flow = y;
next = z;
}
};
arc e[maxn<<2];
int head[maxn],d[maxn],cur[maxn];
int tot,S,T,n,m;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,flow,head[v]);//可以不这么建图。。。。
head[v] = tot++;
}
bool bfs(){
memset(d,-1,sizeof(d));
queue<int>q;
q.push(T);
d[T] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i^1].flow && d[e[i].to] == -1){
d[e[i].to] = d[u] + 1;
q.push(e[i].to);
}
}
}
return d[S] > -1;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = 0,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to]+1==d[u]&&(a=dfs(e[i].to,min(low,e[i].flow)))){
e[i].flow -= a;
e[i^1].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -1;
return tmp;
}
int dinic(){
int ans = 0;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans += dfs(S,INF);
}
return ans;
}
int main() {
int u,v,w,cs = 1;
while(~scanf("%d %d",&n,&m)){
memset(head,-1,sizeof(head));
S = n*m;
T = n*m+1;
for(int i = tot = 0; i < n; ++i)
for(int j = 0; j < m; ++j){
scanf("%d",&u);
if(i) add(m*(i-1)+j,m*i+j,1);
if(j) add(m*i+j-1,m*i+j,1);
if(u == 1) add(S,m*i+j,INF);
if(u == 2) add(m*i+j,T,INF);
}
printf("Case %d:\n%d\n",cs++,dinic());
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: