狼和羊的故事

狼和羊的故事 ⁡ \operatorname{狼和羊的故事}

题目链接: luogu P2598 ⁡ \operatorname{luogu\ P2598} luogu P2598

题目

“狼爱上羊啊爱的疯狂,谁让他们真爱了一场;狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez 听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! Orez 的羊狼圈可以看作一个 n × m n\times m n×m 个矩阵格子,这个矩阵的边缘已经装上了篱笆。可是 Drake 很快发现狼再怎么也是狼,它们总是对羊垂涎三尺,那首歌只不过是一个动人的传说而已。所以 Orez 决定在羊狼圈中再加入一些篱笆,还是要将羊狼分开来养。 通过仔细观察, Orez 发现狼和羊都有属于自己领地,若狼和羊们不能呆在自己的领地,那它们就会变得非常暴躁,不利于他们的成长。 Orez 想要添加篱笆的尽可能的短。当然这个篱笆首先得保证不能改变狼羊的所属领地,再就是篱笆必须修筑完整,也就是说必须修建在单位格子的边界上并且不能只修建一部分。

输入

文件的第一行包含两个整数 n n n m m m 。接下来 n n n 行每行 m m m 个整数, 1 1 1 表示该格子属于狼的领地, 2 2 2 表示属于羊的领地, 0 0 0 表示该格子不是任何一只动物的领地。

输出

文件中仅包含一个整数 a n s ans ans ,代表篱笆的最短长度。

样例输入

2 2
2 2 
1 1 

样例输出

2

数据范围

10 % 10\% 10% 的数据 n , m ≤ 3 n,m≤3 n,m3

30 % 30\% 30% 的数据 n , m ≤ 20 n,m≤20 n,m20

100 % 100\% 100% 的数据 n , m ≤ 100 n,m≤100 n,m100

思路

这道题是一道最小割。

我们可以把每一块领地看成一个点,然后狼的就连向源点,流量无限大。而羊的就连向汇点,流量也是无限大。
然后每一个点可以连向四边的点,流量是 1 1 1

那答案就很明显是最小割了。

那就直接 dinic 算法搞定。

代码

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

struct node {
	int x, to, nxt, op;
}e[1000001];
int n, m, a[101][101], S, T, le[10003], KK, ans, dis[10003];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
queue <int> q;

int getnum(int x, int y) {//求出每个点所对应的编号
	return (x - 1) * m + y;
}

void add(int x, int y, int z) {//网络流建图
	e[++KK] = (node){z, y, le[x], KK + 1}; le[x] = KK;
	e[++KK] = (node){0, x, le[y], KK - 1}; le[y] = KK;
}

bool ch(int x, int y) {//判断点是否还在图内
	if (x < 1 || x > n) return 0;
	if (y < 1 || y > m) return 0;
	return 1;
}

bool bfs() {//dinic算法中的bfs
	while (!q.empty()) q.pop();
	memset(dis, 0x7f, sizeof(dis));
	dis[S] = 0;
	
	q.push(S);
	while (!q.empty()) {
		int now = q.front();
		q.pop();
		
		for (int i = le[now]; i; i = e[i].nxt)
			if (dis[e[i].to] > dis[now] + 1 && e[i].x) {
				dis[e[i].to] = dis[now] + 1;
				if (e[i].to == T) return 1;
				q.push(e[i].to);
			}
	}
	
	return 0;
}

int dfs(int now, int an) {//dinic算法中的dfs
	if (now == T) return an;
	
	int go = 0;
	for (int i = le[now]; i; i = e[i].nxt)
		if (dis[e[i].to] == dis[now] + 1 && e[i].x) {
			int line_go = dfs(e[i].to, min(e[i].x, an - go));
			if (!line_go) dis[e[i].to] = -1;
			e[i].x -= line_go;
			e[e[i].op].x += line_go;
			
			go += line_go;
			if (go == an) break;
		}
	
	return go;
}

void dinic() {//dinic算法
	while (bfs())
		ans += dfs(S, 2147483647);
}

int main() {
	scanf("%d %d", &n, &m);//读入
	
	S = n * m + 1;//给源点和汇点编号
	T = n * m + 2;
	
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			scanf("%d", &a[i][j]);//读入
			if (a[i][j] == 1) add(S, getnum(i, j), 2147483647);//是狼
				else if (a[i][j] == 2) add(getnum(i, j), T, 2147483647);//是羊
			for (int k = 0; k < 4; k++) {
				int tx = i + dx[k], ty = j + dy[k];
				if (!ch(tx, ty)) continue;
				add(getnum(i, j), getnum(tx, ty), 1);//连边
			}
		}
	
	dinic();//dinic算法
	
	printf("%d", ans);//输出
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是用 Java 写的在地图上的示例代码: ``` import java.util.Random; public class Map { private int width; private int height; private boolean[][] occupied; public Map(int width, int height) { this.width = width; this.height = height; this.occupied = new boolean[width][height]; } public boolean isOccupied(int x, int y) { if (x < 0 || x >= this.width || y < 0 || y >= this.height) { return true; } return this.occupied[x][y]; } public void occupy(int x, int y) { if (x >= 0 && x < this.width && y >= 0 && y < this.height) { this.occupied[x][y] = true; } } public void vacate(int x, int y) { if (x >= 0 && x < this.width && y >= 0 && y < this.height) { this.occupied[x][y] = false; } } } public class Animal { protected int x; protected int y; public Animal(int x, int y) { this.x = x; this.y = y; } public void move(int dx, int dy) { this.x += dx; this.y += dy; } public int getX() { return this.x; } public int getY() { return this.y; } } public class Wolf extends Animal { private int energy; public Wolf(int x, int y) { super(x, y); this.energy = 10; } public void eat(Sheep sheep) { this.energy += sheep.getNutritionValue(); } public boolean isStarving() { return this.energy <= 0; } public void loseEnergy() { this.energy--; } public int getEnergy() { return this.energy; } } public class Sheep extends Animal { private int nutritionValue; public Sheep(int x, int y) { super(x, y); this.nutritionValue = 5; } public int getNutritionValue() { return this.nutritionValue; } } public class Simulation { private Map map; private Wolf wolf; private Sheep sheep; private Random random; public Simulation(int width, int height) { this.map = new Map(width, height); this.random = new Random(); int wolfX = this.random.nextInt(width); int wolfY = this.random.nextInt(height); this.wolf = new Wolf(wolfX, wolfY); this.map.occupy(wolfX, wolfY); int sheepX = this.random.nextInt(width); int sheepY = this.random.nextInt(height); while (this.map.isOccupied(sheepX, sheepY)) { sheepX = this.random.nextInt(width); sheepY = this.random.nextInt(height); } this.sheep = new Sheep(sheepX, sheepY); this.map.occupy(sheepX, sheepY); } public void run() { int time = 0; while (!this.wolf.isStarving()) { time++; System.out.println("Time: " + time); System.out.println("Wolf: (" + this.wolf.getX() + ", " + this.wolf.getY() + "), Energy: " + this.wolf.getEnergy()); System.out.println("Sheep: (" + this.sheep.getX() + ", " + this.sheep.getY() + ")"); int dx = this.random.nextInt(3) - 1; int dy = this.random.nextInt(3) - 1; this.wolf.move(dx, dy); if (this.wolf.getX() == this.sheep.getX() && this.wolf.getY() == this.sheep.getY()) { this.wolf.eat(this.sheep); this.map.vacate(this.sheep.getX(), this.sheep.getY()); int newX = this.random.nextInt(this.map.width); int newY = this.random.nextInt(this.map.height); while (this.map.isOccupied(newX, newY)) { newX = this.random.nextInt(this.map.width); newY = this.random.nextInt(this.map.height); } this.sheep = new Sheep(newX, newY); this.map.occupy(newX, newY); } else { this.wolf.loseEnergy(); } System.out.println("============================================="); } System.out.println("Wolf is starving!"); } } public class Main { public static void main(String[] args) { Simulation simulation = new Simulation(10, 10); simulation.run(); } } ``` 以上代码定义了五个类,`Map`、`Animal`、`Wolf`、`Sheep` 和 `Simulation`。`Map` 类代表地图,有 `width`、`height` 和 `occupied` 三个属性,分别代表宽度、高度和哪些位置被占用了;`Animal` 类代表动物,有 `x` 和 `y` 两个属性,分别代表横坐标和纵坐标;`Wolf` 类继承自 `Animal` 类,有 `energy` 属性,代表能量,还有 `eat`、`isStarving`、`loseEnergy` 和 `getEnergy` 方法,分别代表吃、是否饥饿、消耗能量和获取当前能量值;`Sheep` 类继承自 `Animal` 类,有 `nutritionValue` 属性,代表营养价值,还有 `getNutritionValue` 方法,返回营养价值;`Simulation` 类代表仿真,有 `map`、`wolf`、`sheep` 和 `random` 四个属性,分别代表地图、和随机数生成器,还有 `run` 方法,代表运行仿真。 在 `Simulation` 类的构造函数中,初始化地图、。在 `run` 方法中,循环执行以下操作: 1. 输出当前时间、的位置和能量、的位置。 2. 随机生成一个移动的方向,并让移动。 3. 如果在同一个位置,吃掉,并在地图上移除,随机生成一个新的,并在地图上放置。 4. 否则,消耗能量。 5. 输出分割线。 如果的能量小于等于 0,就输出 `Wolf is starving!`。 这只是一个简单的示例,可以根据需要进一步扩展。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值