UVA 11380 - Down Went The Titanic(网络流)

本文探讨了解决UVA11380-DownWentTheTitanic问题的方法,通过最大流算法,将复杂的问题转化为图论问题,并详细解释了拆点、建图、源点和汇点的构建过程,最终利用Dinic算法求解最大流量,实现救援最大人数的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UVA 11380 - Down Went The Titanic

题目链接

题意:给定一个图,上面有薄冰'.'或'*‘,厚冰'@',木块'#',一开始人都在'*'上,薄冰只能走一次就会沉掉,厚冰次数不限,如果人走到木块上就获救了,但是一个木块的容量只有p,求最多能有多少人获救

思路:最大流,由于点有次数限制,所以可以进行拆点,然后建图每个4和四个方向建边,源点和'*'建边,'#'和汇点建边容量为p,然后跑一下最大流即可

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int MAXNODE = 2005;
const int MAXEDGE = 100005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
	int u, v;
	Type cap, flow;
	Edge() {}
	Edge(int u, int v, Type cap, Type flow) {
		this->u = u;
		this->v = v;
		this->cap = cap;
		this->flow = flow;
	}
};

struct Dinic {
	int n, m, s, t;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool vis[MAXNODE];
	Type d[MAXNODE];
	int cur[MAXNODE];
	vector<int> cut;

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}
	void add_Edge(int u, int v, Type cap) {
		edges[m] = Edge(u, v, cap, 0);
		next[m] = first[u];
		first[u] = m++;
		edges[m] = Edge(v, u, 0, 0);
		next[m] = first[v];
		first[v] = m++;
	}

	bool bfs() {
		memset(vis, false, sizeof(vis));
		queue<int> Q;
		Q.push(s);
		d[s] = 0;
		vis[s] = true;
		while (!Q.empty()) {
			int u = Q.front(); Q.pop();
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (!vis[e.v] && e.cap > e.flow) {
					vis[e.v] = true;
					d[e.v] = d[u] + 1;
					Q.push(e.v);
				}
			}
		}
		return vis[t];
	}

	Type dfs(int u, Type a) {
		if (u == t || a == 0) return a;
		Type flow = 0, f;
		for (int &i = cur[u]; i != -1; i = next[i]) {
			Edge& e = edges[i];
			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i^1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0) break;
			}
		}
		return flow;
	}

	Type Maxflow(int s, int t) {
		this->s = s; this->t = t;
		Type flow = 0;
		while (bfs()) {
			for (int i = 0; i < n; i++)
				cur[i] = first[i];
			flow += dfs(s, INF);
		}
		return flow;
	}

	void MinCut() {
		cut.clear();
		for (int i = 0; i < m; i += 2) {
			if (vis[edges[i].u] && !vis[edges[i].v])
				cut.push_back(i);
		}
	}
} gao;

const int N = 35;
const int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

int x, y, p;
char str[N];

int main() {
	while (~scanf("%d%d%d", &x, &y, &p)) {
		int tot = x * y;
		gao.init(2 * tot + 2);
		for (int i = 0; i < x; i++) {
			scanf("%s", str);
			for (int j = 0; j < y; j++) {
				int u = i * y + j + 1;
				if (str[j] == '*' || str[j] == '.') {
					gao.add_Edge(u, u + tot, 1);
					if (str[j] == '*')
						gao.add_Edge(0, u, 1);
				}
				if (str[j] == '#') {
					gao.add_Edge(u, u + tot, INF);
					gao.add_Edge(u + tot, 2 * tot + 1, p);
				}
				if (str[j] == '@') gao.add_Edge(u, u + tot, INF);
				for (int k = 0; k < 4; k++) {
					int xx = i + d[k][0];
					int yy = j + d[k][1];
					if (xx < 0 || xx >= x || yy < 0 || yy >= y) continue;
					int v = xx * y + yy + 1;
					gao.add_Edge(u + tot, v, INF);
				}
			}
		}
		printf("%d\n", gao.Maxflow(0, 2 * tot + 1));
	}
	return 0;
}


### 解决泰坦尼克号生存预测中因变量未定义导致的NameError问题 在处理泰坦尼克号生存预测数据时,如果遇到`NameError: name 'titanic' is not defined`错误,通常是因为变量`titanic`未被正确定义或加载。以下是对该问题的专业解决方案。 #### 数据加载与变量定义 确保数据已正确加载到变量`titanic`中。使用`pandas`库加载CSV文件时,代码应如下所示: ```python import pandas as pd titanic = pd.read_csv('titanic.csv') # 确保文件路径正确[^1] ``` 如果文件路径不正确或文件名拼写错误,将导致`titanic`未被定义。 #### 检查变量是否已定义 在执行任何操作之前,可以检查变量`titanic`是否已定义。例如,通过打印其前几行来验证: ```python print(titanic.head()) # 如果变量未定义,此行将抛出NameError ``` #### 使用`dropna`方法时的注意事项 在调用`dropna`方法之前,必须确保`titanic`变量已被成功加载。以下是正确的`dropna`调用示例: ```python titanic.dropna(inplace=True) # 删除包含缺失值的所有行[^2] ``` 如果在此处出现`NameError`,则表明`titanic`变量未被正确定义。 #### 常见原因及解决方法 1. **文件路径错误**:确保`'titanic.csv'`文件位于当前工作目录下,或者提供完整的文件路径。 ```python titanic = pd.read_csv('/path/to/titanic.csv') # 替换为实际路径 ``` 2. **变量名拼写错误**:检查变量名是否拼写正确。例如,避免将`titanic`误写为`Titanic`或`tiTanic`。 3. **代码执行顺序问题**:确保在调用`titanic.dropna()`之前已成功执行`titanic = pd.read_csv('titanic.csv')`。 #### 示例代码 以下是一个完整的示例代码,展示如何加载数据并删除缺失值: ```python import pandas as pd # 加载数据 titanic = pd.read_csv('titanic.csv') # 确保文件路径正确[^1] # 检查数据是否加载成功 print(titanic.head()) # 删除包含缺失值的行 titanic.dropna(inplace=True) # 删除缺失值[^2] # 输出处理后的数据 print(titanic.info()) ``` #### 注意事项 - 如果`titanic.csv`文件不存在或路径错误,将导致`titanic`变量未定义。 - 在使用`dropna`方法时,确保`titanic`变量已正确定义并加载数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值