Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)
题目链接:
题目描述:
在外星上有n个点需要机器人去探险,有m条单向路径。问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过。
解题思路:
一眼看上去是普通的最小边覆盖,但是仔细想后发现如果在原图上进行最大匹配的话,每个点只能经过一次。这样的话对于本题求出的并不是最优解,所以我们要先对原图进行传递闭包处理,然后再进行最大匹配。
这个题目点数太少H_K和匈牙利算法在空间和时间上并没有什么差,就代码复杂度而言匈牙利算法更有优势。
#include <iostream>//匈牙利算法
#include <cstring>
#include <queue>
#include <cmath>
#include <cstdio>
using namespace std; const int maxn = ;
int maps[maxn][maxn], n, m;
int used[maxn], vis[maxn];
void floyd ()
{
for (int k=; k<=n; k++)
for (int i=; i<=n; i++)
for (int j=; j<=n; j++)
if (maps[i][k] && maps[k][j])
maps[i][j] = ;
}
int Find (int u)
{
for (int i=; i<=n; i++)
{
if (!vis[i] && maps[u][i])
{
vis[i] = ;
if (!used[i] || Find(used[i]))
{
used[i] = u;
return ;
}
}
}
return ;
}
int main ()
{
while (scanf ("%d %d", &n, &m), n||m)
{
memset (maps, , sizeof(maps));
while (m --)
{
int u, v;
scanf ("%d %d", &u, &v);
maps[u][v] = ;
}
floyd ();
memset (used, , sizeof(used));
int res = ;
for (int i=; i<=n; i++)
{
memset (vis, , sizeof(vis));
res += Find(i);
}
printf ("%d\n", n - res);
}
return ;
}
#include <iostream>//H_K算法
#include <cstring>
#include <queue>
#include <cmath>
#include <cstdio>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f;
int maps[maxn][maxn], n, m, dx[maxn], dy[maxn];
int vis[maxn], cx[maxn], cy[maxn], dis;
void floyd ()
{
for (int k=; k<=n; k++)
for (int i=; i<=n; i++)
for (int j=; j<=n; j++)
if (maps[i][k] && maps[k][j])
maps[i][j] = ;
}
bool bfs ()
{
queue <int> Q;
dis = INF;
memset (dx, -, sizeof(dx));
memset (dy, -, sizeof(dy));
for (int i=; i<=n; i++)
if (cx[i] == -)
{
Q.push(i);
dx[i] = ;
}
while (!Q.empty())
{
int u = Q.front();
Q.pop();
if (dx[u] > dis)
break;
for (int i=; i<=n; i++)
{
if (dy[i]==- && maps[u][i])
{
dy[i] = dx[u] + ;
if (cy[i] == -)
dis = dy[i];
else
{
dx[cy[i]] = dy[i] +;
Q.push (cy[i]);
}
}
}
}
return dis != INF;
}
int dfs (int u)
{
for (int v=; v<=n; v++)
{
if (!vis[v] && dx[u]+==dy[v] && maps[u][v])
{
vis[v] = ;
if (cy[v]!=- && dis==dy[v])
continue;
if (cy[v]==- || dfs(cy[v]))
{
cx[u] = v;
cy[v] = u;
return ;
}
}
}
return ;
}
int Max_match ()
{
int res = ;
memset (cx, -, sizeof(cx));
memset (cy, -, sizeof(cy));
while (bfs())
{
memset (vis, , sizeof(vis));
for (int i=; i<=n; i++)
if (cx[i] == -)
res += dfs (i);
}
return res;
}
int main ()
{
while (scanf ("%d %d", &n, &m), n||m)
{
memset (maps, , sizeof(maps));
while (m --)
{
int u, v;
scanf ("%d %d", &u, &v);
maps[u][v] = ;
}
floyd ();
printf ("%d\n", n - Max_match());
}
return ;
}
Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)的更多相关文章
- poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)
http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total ...
- POJ 2594 Treasure Exploration(最小路径覆盖变形)
POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...
- POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
Treasure Exploration Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- poj 2594 Treasure Exploration (二分匹配)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 6558 Accepted: 2 ...
- POJ 2594 Treasure Exploration 最小可相交路径覆盖
最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如 ...
- poj 2594 Treasure Exploration 二分图匹配
点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7215 ...
- POJ 2594 Treasure Exploration (可相交最小路径覆盖)
题意 给你张无环有向图,问至少多少条路径能够覆盖该图的所有顶点--并且,这些路径可以有交叉. 思路 不是裸的最小路径覆盖,正常的最小路径覆盖中两个人走的路径不能有重复的点,而本题可以重复. 当然我们仍 ...
- POJ 2594 Treasure Exploration (Floyd+最小路径覆盖)
<题目链接> 题目大意: 机器人探索宝藏,有N个点,M条边.问你要几个机器人才能遍历所有的点. 解题分析: 刚开始还以为是最小路径覆盖的模板题,但是后面才知道,本题允许一个点经过多次,这与 ...
- POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)
题意: 派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.有的点可以重复去. 输入数据: 首先是n和 ...
随机推荐
- Failed to execute 'toDataURL' on 'HTMLCanvasElement,在canvas.toDataURL()执行时候报错解决方案
添加跨域条件 crossorigin="anonymous" [Redirect at origin 'http://xxx.xx.com' has been blocked ...
- 聊聊高并发(四十)解析java.util.concurrent各个组件(十六) ThreadPoolExecutor源代码分析
ThreadPoolExecutor是Executor运行框架最重要的一个实现类.提供了线程池管理和任务管理是两个最主要的能力.这篇通过分析ThreadPoolExecutor的源代码来看看怎样设计和 ...
- Deepin-安装vscode
安装方式有两种: 1.通过命令安装 sudo apt-get install vscode 2.通过deb或rpm包安装 我们是Debian系列的系统,所以用deb包,关于红帽系统,请使用rpm包. ...
- 分享一个非常屌的eazyui二开demo
eazyui二开Demo非常吊,里面各种非常吊的样例,最喜欢的是 多文件进度条上传,一次可多选,还有流程,还有文本编辑器 非常简洁的 不像一些官网各种复杂的东西.主要为自己保留一份, 在线demo在 ...
- bootstrap中固定table的表头
前端时间鼓捣的一个简单的手机站点,今天又拿出来弄一弄 由于主要是给手机訪问.用的是bootstrap响应式布局,今天的任务是做一个数据展示页面 可是因为数据的列比較多.所以横向显示不下,为了较好的显示 ...
- Angular45
Angular 4 Tutorial for Beginners: Learn Angular 4 from Scratch https://www.youtube.com/watch?v=k5E2A ...
- vs2010下配置CUDA出现kernel launch failed问题,内核无效
首先, 推荐一篇不错的配置文档~手把手教你 CUDA 5.5与VS2010编译环境的搭建.笔者就是在这篇文章的指导下成功地在VS2010上搭建了CUDA 6.5~ 其次. 文末给出的执行演示样例不好使 ...
- MFC程序打开文件对话框出错的问题解决
前几天从网上下了个图像分析的mfc小程序,是VC6的 用VC6在本地编译生成都没问题.执行起来弹出一个未处理的错误,程序崩溃退出. 想起来原来遇到过打开文件对话框方面的问题,当时项目时间紧张未能深究. ...
- WIN7 不用格式化磁盘怎么把FAT32系统改成NTFS系统
开始-运行,输入cmd回车.假设你要转换D盘.输入convert d: /fs:NTFS回车. [ 此时可能会提示: 访问被拒绝 因为你没有足够的特权 是权限不够的原因 开始--程序--附件 右键&q ...
- [BZOJ2144]国家集训队 跳跳棋
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...