Treasure Exploration

本文介绍了一种用于最小化机器人数量的算法,旨在探索火星未知区域的每个角落。通过使用图论和匹配算法,该方法能够确定最少的机器人需求,以确保所有地点都能被覆盖。文章详细解释了算法流程,包括输入输出格式,以及一个示例输入输出。

题目描述:

Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you.
Recently, a company named EUC (Exploring the Unknown Company) plan to explore an unknown place on Mars, which is considered full of treasure. For fast development of technology and bad environment for human beings, EUC sends some robots to explore the treasure.
To make it easy, we use a graph, which is formed by N points (these N points are numbered from 1 to N), to represent the places to be explored. And some points are connected by one-way road, which means that, through the road, a robot can only move from one end to the other end, but cannot move back. For some unknown reasons, there is no circle in this graph. The robots can be sent to any point from Earth by rockets. After landing, the robot can visit some points through the roads, and it can choose some points, which are on its roads, to explore. You should notice that the roads of two different robots may contain some same point.
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars.
As an ICPCer, who has excellent programming skill, can your help EUC?

输入:

The input will consist of several test cases. For each test case, two integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given in the first line, indicating the number of points and the number of one-way roads in the graph respectively. Each of the following M lines contains two different integers A and B, indicating there is a one-way from A to B (0 < A, B <= N). The input is terminated by a single line with two zeros.

输出:

For each test of the input, print a line containing the least robots needed.

样例输入:

1 0
2 1
1 2
2 0
0 0

样例输出:

1
1
2

code:

有是一个网络流,发现纯套模板果然是不行的

然后错了十几次吧

总算是对了

目前还在理解,有机会更新

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int n,m;
const int maxn=650;
int match[maxn];
bool visit[maxn];
int a[maxn][maxn];
bool dfs(int v){
	for(int i=1;i<=n;i++){
		if(a[v][i]==1&&visit[i]==0){
			visit[i]=1;
			if(match[i]<0||dfs(match[i])){
				match[i]=v;
				return true;
			}
		}
	}
	return false;
}
int findd(){
	int res=0;
	memset(match,-1,sizeof(match));
	for(int v=1;v<=n;v++){
		memset(visit,0,sizeof(visit));
		if(dfs(v))res++;
	}
	return res;
}
int main()
{
	while(~scanf("%d%d",&n,&m)){
		if(n==0&&m==0)break;
		int u,v;
		memset(a,0,sizeof(a));
		while(m--){
			scanf("%d%d",&u,&v);
			a[u][v]=1;
		}
		for(int k=1;k<=n;k++){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(a[i][k]==1&&a[k][j]==1)
						a[i][j]=1;
				}
			}
		}
		printf("%d\n",n-findd());
	}
	return 0;
 } 

 

### 最小路径覆盖问题及其解决方案 最小路径覆盖问题是图论中一个经典的问题,尤其在有向无环图(DAG)中有着广泛的应用。该问题的目标是找到图中尽可能少的路径,使得这些路径能够覆盖所有顶点,并且每个顶点恰好出现在一条路径中。 #### 问题定义 对于给定的有向图 $ G = (V, E) $,设 $ P $ 是 $ G $ 的一个简单路径集合。如果 $ V $ 中的每个顶点恰好在 $ P $ 的一条路径上,则称 $ P $ 是 $ G $ 的一个路径覆盖。目标是找到路径数最少的路径覆盖,即最小路径覆盖。 #### 解决方案:网络流模型 解决最小路径覆盖问题的经典方法是将其转化为最大匹配问题,进而通过网络流算法求解。 ##### DAG中的最小路径覆盖 对于有向无环图(DAG),可以通过构造二分图并求其最大匹配来解决最小路径覆盖问题。具体步骤如下: 1. **构建二分图**: - 将原图中的每个顶点 $ v $ 拆分为两个部分:$ v_{in} $ 和 $ v_{out} $。 - 左侧集合为 $ \{v_{in} | v \in V\} $,右侧集合为 $ \{v_{out} | v \in V\} $。 - 对于每条边 $ (u, v) \in E $,在二分图中添加一条从 $ u_{in} $ 到 $ v_{out} $ 的边。 2. **求最大匹配**: - 在构造的二分图中求最大匹配。最大匹配的大小记为 $ M $。 3. **计算最小路径覆盖**: - 最小路径覆盖的大小等于顶点数减去最大匹配的大小,即 $ |V| - M $ [^4]。 ##### 示例代码 以下是一个基于匈牙利算法实现的最大匹配求解代码片段,用于计算 DAG 的最小路径覆盖: ```cpp #include <cstdio> #include <cstring> using namespace std; #define N 150 int lover[N]; // 记录匹配关系 bool vis[N]; // 标记访问状态 int e[N][N]; // 邻接矩阵表示二分图 bool find(int x, int n) { for (int i = 1; i <= n; i++) { if (e[x][i] && !vis[i]) { vis[i] = true; if (!lover[i] || find(lover[i], n)) { lover[i] = x; return true; } } } return false; } int max_matching(int n) { int ans = 0; memset(lover, 0, sizeof(lover)); for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); if (find(i, n)) ans++; } return ans; } int main() { int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d %d", &n, &m); memset(e, 0, sizeof(e)); for (int i = 1; i <= m; i++) { int a, b; scanf("%d %d", &a, &b); e[a][b] = 1; // 构造二分图 } int match = max_matching(n); printf("最小路径覆盖数: %d\n", n - match); } return 0; } ``` #### 可相交的最小路径覆盖 在某些情况下,路径可以相交。例如,在 POJ 2594 Treasure Exploration 问题中,允许路径在顶点上重叠。这种情况下,可以通过先对图进行传递闭包处理,再使用上述方法求解最小路径覆盖 [^3]。 #### 应用场景 - **任务调度**:将任务之间的依赖关系建模为有向图,寻找最少的任务序列以覆盖所有任务。 - **软件工程**:在模块调用图中,确定最少的测试路径以覆盖所有模块。 - **交通规划**:设计最少的路线以覆盖所有地点。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值