[ZJOI2009]狼和羊的故事 BZOJ1412
题目描述
“狼爱上羊啊爱的疯狂,谁让他们真爱了一场;狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! Orez的羊狼圈可以看作一个n*m个矩阵格子,这个矩阵的边缘已经装上了篱笆。可是Drake很快发现狼再怎么也是狼,它们总是对羊垂涎三尺,那首歌只不过是一个动人的传说而已。所以Orez决定在羊狼圈中再加入一些篱笆,还是要将羊狼分开来养。 通过仔细观察,Orez发现狼和羊都有属于自己领地,若狼和羊们不能呆在自己的领地,那它们就会变得非常暴躁,不利于他们的成长。 Orez想要添加篱笆的尽可能的短。当然这个篱笆首先得保证不能改变狼羊的所属领地,再就是篱笆必须修筑完整,也就是说必须修建在单位格子的边界上并且不能只修建一部分。
输入输出格式
输入格式:
文件的第一行包含两个整数n和m。接下来n行每行m个整数,1表示该格子属于狼的领地,2表示属于羊的领地,0表示该格子不是任何一只动物的领地。
输出格式:
文件中仅包含一个整数ans,代表篱笆的最短长度。
输入输出样例
说明
数据范围
10%的数据 n,m≤3
30%的数据 n,m≤20
100%的数据 n,m≤100
最小割;
源点和狼连边,容量为inf;
汇点与羊连边,容量为inf;
接着所有的点再与周围的点相连,容量为1;
求最小割时自然会割去容量为1的边,最终使得st,ed分开;
那么也就是我们的目的所在;
所以直接跑一边dinic就行了;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 400005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n, m;
int st, ed;
struct node {
int u, v, nxt, w;
}edge[maxn<<1]; int head[maxn], cnt; void addedge(int u, int v, int w) {
edge[cnt].u = u; edge[cnt].v = v; edge[cnt].w = w;
edge[cnt].nxt = head[u]; head[u] = cnt++;
} int rk[maxn];
int bfs() {
queue<int>q; ms(rk); rk[st] = 1; q.push(st);
while (!q.empty()) {
int tmp = q.front(); q.pop();
for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
int to = edge[i].v;
if (rk[to] || edge[i].w <= 0)continue;
rk[to] = rk[tmp] + 1; q.push(to);
}
}
return rk[ed];
} int dfs(int u, int flow) {
if (u == ed)return flow;
int add = 0;
for (int i = head[u]; i != -1 && add < flow; i = edge[i].nxt) {
int v = edge[i].v;
if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
int tmpadd = dfs(v, min(edge[i].w, flow - add));
if (!tmpadd) { rk[v] = -1; continue; }
edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd; add += tmpadd;
}
return add;
} int ans;
void dinic() {
while (bfs())ans += dfs(st, inf);
} int pos(int x, int y) {
return m * (x - 1) + y;
}
int mp[200][200];
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 }; int main()
{
//ios::sync_with_stdio(0);
rdint(n); rdint(m); memset(head, -1, sizeof(head));
st = m * n + 1; ed = st + 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)rdint(mp[i][j]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] == 1) {
addedge(st, pos(i, j), inf);
addedge(pos(i, j), st, 0);
}
else if (mp[i][j] == 2) {
addedge(pos(i, j), ed, inf);
addedge(ed, pos(i, j), 0);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 0; k < 4; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
addedge(pos(i, j), pos(nx, ny), 1);
addedge(pos(nx, ny), pos(i, j), 0);
}
}
}
}
dinic();
cout << ans << endl;
return 0;
}
[ZJOI2009]狼和羊的故事 BZOJ1412的更多相关文章
- 【BZOJ1412】[ZJOI2009]狼和羊的故事 最小割
[BZOJ1412][ZJOI2009]狼和羊的故事 Description “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想: ...
- BZOJ1412 ZJOI2009 狼和羊的故事 【网络流-最小割】
BZOJ1412 ZJOI2009 狼和羊的故事 Description “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和 ...
- 嘴巴题3 「BZOJ1412」[ZJOI2009] 狼和羊的故事
「BZOJ1412」[ZJOI2009] 狼和羊的故事 Description "狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......" Or ...
- bzoj1412: [ZJOI2009]狼和羊的故事
空地之间开始没有连然后一直WA...题意混乱...尴尬. #include<cstdio> #include<cstring> #include<iostream> ...
- BZOJ1412 [ZJOI2009]狼和羊的故事 【最小割】
1412: [ZJOI2009]狼和羊的故事 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3454 Solved: 1733 [Submit][ ...
- BZOJ 1412: [ZJOI2009]狼和羊的故事( 最小割 )
显然是最小割...把狼的领地连S, 羊的领地连T, 然后中间再连边, 跑最大流就OK了 -------------------------------------------------------- ...
- P2598 [ZJOI2009]狼和羊的故事(网络流)
P2598 [ZJOI2009]狼和羊的故事 源点和所有狼连 $inf$ 的边 所有羊和汇点连 $inf$ 的边 所有点向四周连 $1$ 的边 这样所有狼和羊之间的边都被割掉了 统计最小割就好辣 #i ...
- 洛谷 P2598 [ZJOI2009]狼和羊的故事 解题报告
P2598 [ZJOI2009]狼和羊的故事 题目描述 "狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......" \(Orez\)听到这首歌, ...
- 题解 P2598 【[ZJOI2009]狼和羊的故事】
P2598 [ZJOI2009]狼和羊的故事 题目描述 "狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......" Orez听到这首歌,心想:狼 ...
随机推荐
- List转Datable(需区分对象充当List成员和数组充当List成员两种情况)
对象充当List成员时: /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name ...
- C++ 学习之---C++基础理论知识(也适合面试回顾)
C++ 语言编写的基础练习 具体案例放在github中 github地址:https://github.com/Master-fd/C-Base 1. 操作符重载 2. 构造与析构 3. 函数模板 4 ...
- JDBC批处理数据
JDBC3.0 的增强支持BLOB,CLOB,ARRAY,REF数据类型.的ResultSet对象UPDATEBLOB(),updateCLOB(),updateArray()和updateRef( ...
- iOS 给Main.storyboard 添加button 事件《转》
XCODE中使用Main.Storyboard拉入控件并实现事件(Swift语言) 如何在XCODE中的Main.Storyboard内拉入控件并实现一个简单的效果呢?本人由于刚接触Swift语言 ...
- 连接ORACLE客户端工具navicat111.12 for oracle
安装navicat111.12 for oracle后 打开
- 2018网络预选赛 徐州H 线段树+树状数组
设读入的数组是a,树状数组用来维护a数组区间和sum,线段树用来维护一个另一个数组ssum的区间和,区间每个点a[i]*(n-i+1),那么l-r的答案是l-r的ssum-(n-r)*(sum[r]- ...
- php中用大括号把?>和<?php框起来的作用
<?php function my_function() { ?> My function was called <!--就是这里,为什么前面要用?>和< ?php 把M ...
- ruby 【rails在win7_64位操作系统安装】
gem update --system --source http://production.s3.rubygems.org 安装截图
- 在PyCharm 软件中设置你的项目 使用的Python版本
在PyCharm 软件中设置你的项目 使用的Python版本 python2 和 python3 有很大的不同,使用python2 编写的程序,如果使用python3 就运行不了:使用python3编 ...
- c++正确处理 is-a has-a关系
比如.我们想实现一个Set类,而已经有一个List类可提供给你使用,我们到底用is-a(public继承)关系还是用has-a(组合)关系呢? 1:如果使用is-a关系,则 class Set:pub ...