POJ 1422 Air Raid (二分图最小路径覆盖)

本文探讨了在无环有向图中,如何利用最少数量的伞兵遍历所有城镇的问题。通过将问题转化为最小路径覆盖,即在图中寻找尽量少的路径使每个节点恰好在一条路径上,从而得出伞兵数量等于城镇数减去最大匹配数的结论。

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town’s streets you can never reach the same intersection i.e. the town’s streets form no cycles.

With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.
Input
Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:

no_of_intersections
no_of_streets
S1 E1
S2 E2

Sno_of_streets Eno_of_streets

The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town’s streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.

There are no blank lines between consecutive sets of data. Input data are correct.
Output
The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town.
Sample Input
2
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3
Sample Output
2
1
**题意:t组数据,对于每组数据,n代表城镇的数目,m代表单向道路数;接下来m行:u v 表示u到v有一条单向路。
现在派伞兵去遍历所有城镇,每个伞兵可以走遍它能走到的任何地方,求最小需要派多少伞兵。
题解:这是个最小路径覆盖问题,在DAG图中寻找尽量少的路径,使得每个节点恰好在一条路径上(不同的路径不可能有公共点)。我们可以发现每匹配一条路径派的伞兵就可以少一个,所以答案等于n-边最大匹配数
**

#include<stdio.h>
#include<string.h>
int n,m,ans;
int g[350][350];
int pipei[350];
int used[350];
int dfs(int x)
{
	for(int i=1;i<=n;i++)
	{
		int y=g[x][i];
		if(y&&!used[i])
		{
			used[i]=1;
			if(pipei[i]==-1||dfs(pipei[i]))
			{
				pipei[i]=x;
				return 1;
			}
		}
	}
	return 0;
}
int solve()
{
	for(int i=1;i<=n;i++)
	{
		memset(used,0,sizeof(used));
		ans+=dfs(i);
	}
}
int main()
{
	int t,u,v;
	scanf("%d",&t);
	while(t--)
	{
		ans=0;
		memset(pipei,-1,sizeof(pipei));
		memset(g,0,sizeof(g));
		scanf("%d %d",&n,&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d %d",&u,&v);
			g[u][v]=1;
		}
		solve();
		printf("%d\n",n-ans);
	}
	
	return 0;
} 
这个是完整源码 python实现 Django 【python毕业设计】基于Python的天气预报(天气预测分析)(Django+sklearn机器学习+selenium爬虫)可视化系统.zip 源码+论文+sql脚本 完整版 数据库是mysql 本研究旨在开发一个基于Python的天气预报可视化系统,该系统结合了Django框架、sklearn机器学习库和Selenium爬虫技术,实现对天气数据的收集、分析和可视化。首先,我们使用Selenium爬虫技术从多个天气数据网站实时抓取气象数据,包括温度、湿度、气压、风速等多项指标。这些数据经过清洗和预处理后本研究旨在开发一个基于Python的天气预报可视化系统,该系统结合了Django框架、sklearn机器学习库和Selenium爬虫技术,实现对天气数据的收集、分析和可视化。首先,我们使用Selenium爬虫技术从多个天气数据网站实时抓取气象数据,包括温度、湿度、气压、风速等多项指标。这些数据经过清洗和预处理后,将其存储在后端数据库中,以供后续分析。 其次,采用s,将其存储在后端数据库中,以供后续分析。 其次,采用sklearn机器学习库构建预测模型,通过时间序列分析和回归方法,对未来天气情况进行预测。我们利用以往的数据训练模型,以提高预测的准确性。通过交叉验证和超参数优化等技术手段,我们优化了模型性能,确保其在实际应用中的有效性和可靠性。 最后,基于Django框架开发前端展示系统,实现天气预报的可视化。用户可以通过友好的界面查询实时天气信息和未来几天内的天气预测。系统还提供多种图表类型,包括折线图和柱状图,帮助用户直观理解天气变化趋势。 本研究的成果为天气预报领域提供了一种新的技术解决方案,不仅增强了数据获取和处理的效率,还提升了用户体验。未来,该系统能够扩展至其他气象相关的应用场景,为大众提供更加准确和及时的气象服务。
### 关于二分图最小顶点覆盖的算法实现 #### 1. 最小顶点覆盖的概念 最小顶点覆盖是指在一个二分图中选取尽可能少的节点,使得这些节点能够覆盖所有的边。换句话说,对于每一条边 (u, v),至少有一个端点 u 或 v 被选入覆盖集中。 根据 König 定理,在任意无向二分图中,最小顶点覆盖的数量等于该图的最大匹配数量[^1]。 --- #### 2. 算法原理 为了求解二分图最小顶点覆盖,通常采用 **匈牙利算法** 来计算最大匹配数。具体过程如下: - 构建一个二分图 G(X,Y,E),其中 X 和 Y 是两个互不相交的节点集合,E 表示连接它们的边。 - 使用匈牙利算法找到二分图的最大匹配 M。 - 基于最大匹配的结果,通过以下方式构造最小顶点覆盖- 将左侧未被匹配的节点加入到覆盖集中; - 对右侧已被匹配的节点也加入到覆盖集中。 最终得到的覆盖集大小即为最小顶点覆盖的数量[^2]。 --- #### 3. 实现代码 以下是基于 Python 的实现代码,利用匈牙利算法完成二分图最小顶点覆盖的计算: ```python from collections import defaultdict def hungarian_algorithm(graph, n, m): """ 匈牙利算法用于寻找二分图的最大匹配 :param graph: 邻接表表示的二分图 {X -> [Y]} :param n: 左侧节点数目 :param m: 右侧节点数目 :return: 最大匹配结果 """ match_y = [-1] * m # 记录右侧节点对应的匹配关系 visited = None # 当前轮次访问标记 def dfs(u): for v in graph[u]: if not visited[v]: visited[v] = True if match_y[v] == -1 or dfs(match_y[v]): match_y[v] = u return True return False matching_count = 0 for i in range(n): visited = [False] * m if dfs(i): matching_count += 1 return matching_count, match_y def min_vertex_cover(graph, n, m): """ 求解二分图最小顶点覆盖 :param graph: 邻接表表示的二分图 {X -> [Y]} :param n: 左侧节点数目 :param m: 右侧节点数目 :return: 最小顶点覆盖集合 """ max_matching, match_y = hungarian_algorithm(graph, n, m) cover_x = set() # 左侧需要覆盖的节点 cover_y = set() # 右侧需要覆盖的节点 unmatched_in_x = set(range(n)) # 初始认为所有左侧节点都未匹配 matched_in_y = set() for y in range(m): # 找出右侧已匹配的节点 if match_y[y] != -1: unmatched_in_x.discard(match_y[y]) # 如果某个左侧节点参与了匹配,则移除 matched_in_y.add(y) cover_x.update(unmatched_in_x) # 添加左侧未匹配的节点 cover_y.update(set(range(m)) - matched_in_y) # 添加右侧未匹配的节点 return list(cover_x), list(cover_y) # 测试用例 if __name__ == "__main__": # 输入邻接表形式的二分图 graph = { 0: [0, 1], 1: [0, 2], 2: [1, 3] } n = 3 # 左侧节点数 m = 4 # 右侧节点数 result_x, result_y = min_vertex_cover(graph, n, m) print(f"左侧需覆盖的节点: {result_x}") print(f"右侧需覆盖的节点: {result_y}") ``` 上述代码实现了二分图最小顶点覆盖功能,核心部分依赖匈牙利算法来获取最大匹配,并据此推导出覆盖所需的节点集合[^3]。 --- #### 4. 应用实例 考虑 POJ 3041 Asteroids 这道题目,其本质是一个二分图最小顶点覆盖问题。给定一组障碍物坐标,将其转化为二分图模型后,可以通过以上方法高效解决。最终输出的是满足条件的最小射击次数[^4]。 ---
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值