UVA_11380_Down Went The Titanic

本文介绍了一个基于网络流算法的应用实例,通过定义特定的图结构来解决特定问题。该程序使用了多种C++标准库,包括容器和算法,并实现了深度优先搜索以寻找增广路径并更新流量,最终求得最大流。
#include<iostream>  
#include<sstream>  
#include<string>  
#include<vector>  
#include<list>  
#include<set>  
#include<map>
#include<stack>  
#include<queue>  
#include<algorithm>  
#include<numeric>  
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#pragma warning(disable:4996)  
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
const int MaxVexNum = 2000;
const int INF = 1000000000;
class Edge
{
public:
	int to, capacity, flow;
	Edge(int t, int c, int f)
	{
		to = t,capacity = c,flow = f;
	}
};
vector<pair<int, int>>direction{ {0,-1},{0,1},{1,0},{-1,0} };
class Network
{
private:
	int vexnum, edgenum, source, destination,maxflow;
	vector<vector<Edge>>adjList;
public:
	void addEdge(int from, int to, int cap)
	{
		adjList[from].push_back({ to, cap, (int)adjList[to].size() });
		adjList[to].push_back({ from, 0, (int)adjList[from].size() - 1 });
	}
	Network()
	{
		adjList.resize(MaxVexNum);
		int row, column, people;
		cin >> row >> column >> people;
		source = 0, destination = 2 * row*column + 1;
		vector<vector<char>>graph(row+1,(vector<char>)(column+1));
		for (int i = 1; i <= row; i++)
		{
			for (int j = 1; j <= column; j++)
			{
				cin >> graph[i][j];
			}
		}
		for (int i = 1; i <= row; i++)
		{
			for (int j = 1; j <= column; j++)
			{
				auto first = (i - 1)*column + j;
				auto second = first + row*column;
				switch (graph[i][j])
				{
				case '*':addEdge(source,first,1); addEdge(first, second, 1); break;
				case '.':addEdge(first, second, 1); break;
				case '@':addEdge(first, second, INF); break;
				case '#':addEdge(second, destination, people); addEdge(first, second, INF); break;
				default:break;
				}
				for (int k = 0; k < 4; k++)
				{
					int x = i + direction[k].first, y = j + direction[k].second;
					if (x >= 1 && x <= row&&y >= 1 && y <= column)
					{
						auto FIRST = (x - 1)*column + y;
						switch (graph[x][y])
						{
						case '*':addEdge(second, FIRST, 1);break;
						case '.':addEdge(second, FIRST, 1); break;
						case '@':addEdge(second, FIRST, INF); break;
						case '#':addEdge(second, FIRST, INF); break;
						default:break;
						}
					}
				}
			}
		}
	}
	int dfs(int src, int sink, int flow,vector<bool>&used)
	{
		if (src == sink)
		{
			return flow;
		}
		used[src] = true;
		for (int i = 0; i<adjList[src].size(); i++)
		{
			Edge &edge = adjList[src][i];
			if (!used[edge.to] && edge.capacity>0)
			{
				int minflow = dfs(edge.to, sink, std::min(flow, edge.capacity),used);
				if (minflow>0)
				{
					edge.capacity -= minflow;
					adjList[edge.to][edge.flow].capacity += minflow;
					return minflow;
				}
			}
		}
		return 0;
	}
	void getMaxFlow()
	{
		maxflow = 0;
		while (1)
		{
			vector<bool>used(MaxVexNum);
			int f = dfs(source, destination, INF,used);
			if (!f)
			{
				return;
			}
			maxflow += f;
		}
	}
	void print()
	{
		cout << maxflow << endl;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	char ch; 
	while ((ch=cin.get())!=EOF)
	{
		ungetc(ch,stdin);
		Network network;
		network.getMaxFlow();
		network.print();
		cin.get();
	}
	return 0;
}


在处理类似 Titanic 数据集时,通常会使用 `pandas` 库来加载和查看数据集的详细信息。调用 `titanic_df.info()` 方法可以输出数据框中各列的信息,包括列名、非空值数量以及对应的数据类型。 以下是一个典型的 `titanic_df.info()` 输出示例: ``` <class 'pandas.core.frame.DataFrame'> RangeIndex: 891 entries, 0 to 890 Data columns (total 12 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 PassengerId 891 non-null int64 1 Survived 891 non-null int64 2 Pclass 891 non-null int64 3 Name 891 non-null object 4 Sex 891 non-null object 5 Age 714 non-null float64 6 SibSp 891 non-null int64 7 Parch 891 non-null int64 8 Ticket 891 non-null object 9 Fare 891 non-null float64 10 Cabin 204 non-null object 11 Embarked 889 non-null object ``` 上述输出说明: - **PassengerId**: 每位乘客的唯一标识符,共 891 条非空记录。 - **Survived**: 表示是否生存(0 = 死亡,1 = 生存),也是完整的数据。 - **Pclass**: 船舱等级(1 = 一等舱,2 = 二等舱,3 = 三等舱)。 - **Name**: 乘客姓名,无缺失值。 - **Sex**: 性别字段,同样没有缺失值。 - **Age**: 年龄字段,有 714 条非空记录,意味着部分年龄数据缺失[^1]。 - **SibSp**: 兄弟姐妹或配偶人数。 - **Parch**: 父母与子女的数量。 - **Ticket**: 船票编号。 - **Fare**: 船票价格。 - **Cabin**: 客舱号,仅有 204 条有效记录,表明该字段存在大量缺失值。 - **Embarked**: 登船港口(S, C 或 Q),只有 889 条有效记录,有 2 条缺失。 如果希望将此信息用于进一步分析,可以通过如下方式获取: ```python import pandas as pd # 加载 Titanic 数据集 titanic_df = pd.read_csv("titanic.csv") # 查看数据集的基本信息 print(titanic_df.info()) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值