>Description
羊狼圈是一个n*m个矩阵格子,这个矩阵的边缘已经装上了篱笆。在羊狼圈中再加入一些篱笆,将羊狼分开。狼和羊都有属于自己领地,若狼和羊们不能呆在自己的领地,那它们就会变得非常暴躁,不利于他们的成长。 Orez想要添加篱笆的尽可能的短。当然这个篱笆首先得保证不能改变狼羊的所属领地,再就是篱笆必须修筑完整,也就是说必须修建在单位格子的边界上并且不能只修建一部分。
>Input
文件的第一行包含两个整数n和m。接下来n行每行m个整数,1表示该格子属于狼的领地,2表示属于羊的领地,0表示该格子不是任何一只动物的领地。
>Output
文件中仅包含一个整数ans,代表篱笆的最短长度。
数据范围
10%的数据 n,m≤3
30%的数据 n,m≤20
100%的数据 n,m≤100
>解题思路
最小割,也就是最大流完成
边权为1很容易想到,因为在相邻的两格中间建篱笆的代价为1
这时我们只要求S~T的最小割就可以完成
乱乱的改了好久TT
>代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 110
#define inf 1 << 30
#define S n * m + 1
#define T n * m + 2
#define int long long
using namespace std;
const int xx[4] = {-1, 0, 0, 1}, yy[4] = {0, -1, 1, 0};
struct line
{
int to, next, w, op;
} a[240005];
int n, m, ans, t, s[N][N], h[N * N], c[N * N];
bool yd[N * N];
void add (int x, int y, int ww)
{
a[++t] = (line) {y, h[x], ww, t + 1}; h[x] = t; //顺流
a[++t] = (line) {x, h[y], 0, t - 1}; h[y] = t; //逆流
}
bool spfa ()
{
queue<int> st;
memset (c, 0x7f, sizeof (c));
memset (yd, 0, sizeof (yd));
c[S] = 0; yd[S] = 1;
st.push(S);
while (!st.empty())
{
int u = st.front();
for (int i = h[u]; i; i = a[i].next)
if (a[i].w && c[u] + 1 < c[a[i].to])
{
c[a[i].to] = c[u] + 1;
if (a[i].to == T) return 1; //如果S有路径可以到达T,直接返回1
if (!yd[a[i].to])
{
yd[a[i].to] = 1;
st.push(a[i].to);
}
}
yd[u] = 0;
st.pop();
}
return 0;
}
int dfs (int now, int maxf)
{
if (now == T) return maxf;
int ret = 0;
for (int i = h[now]; i; i = a[i].next)
if (a[i].w && c[now] + 1 == c[a[i].to])
{
int f = dfs (a[i].to, min (maxf - ret, a[i].w)); //求最小割
a[i].w -= f;
a[a[i].op].w += f;
ret += f;
if (ret == maxf) return ret; //优化
}
return ret;
}
int dinic () //dinic算法
{
int ans = 0;
while (spfa ()) ans += dfs (S, inf); //如果有增广路就进行增广
return ans;
}
signed main()
{
scanf ("%lld%lld", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
scanf ("%lld", &s[i][j]);
if (s[i][j] == 1) add (S, (i - 1) * m + j, inf);
else if (s[i][j] == 2) add ((i - 1) * m + j, T, inf);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (s[i][j] == 0 || s[i][j] == 1)
for (int k = 0; k < 4; k++) //四周
{
int x = i + xx[k], y = j + yy[k];
if (x < 1 || x > n || y < 1 || y > m) continue;
if (s[x][y] == 2 || s[x][y] == 0)
add ((i - 1) * m + j, (x - 1) * m + y, 1);
}
printf ("%lld", dinic ());
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容