题目链接:

  Poj 2594 Treasure Exploration

题目描述:

  在外星上有n个点需要机器人去探险,有m条单向路径。问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过。

解题思路:

  一眼看上去是普通的最小边覆盖,但是仔细想后发现如果在原图上进行最大匹配的话,每个点只能经过一次。这样的话对于本题求出的并不是最优解,所以我们要先对原图进行传递闭包处理,然后再进行最大匹配。

这个题目点数太少H_K和匈牙利算法在空间和时间上并没有什么差,就代码复杂度而言匈牙利算法更有优势。

  1. #include <iostream>//匈牙利算法
  2. #include <cstring>
  3. #include <queue>
  4. #include <cmath>
  5. #include <cstdio>
  6. using namespace std;
  7.  
  8. const int maxn = ;
  9. int maps[maxn][maxn], n, m;
  10. int used[maxn], vis[maxn];
  11. void floyd ()
  12. {
  13. for (int k=; k<=n; k++)
  14. for (int i=; i<=n; i++)
  15. for (int j=; j<=n; j++)
  16. if (maps[i][k] && maps[k][j])
  17. maps[i][j] = ;
  18. }
  19. int Find (int u)
  20. {
  21. for (int i=; i<=n; i++)
  22. {
  23. if (!vis[i] && maps[u][i])
  24. {
  25. vis[i] = ;
  26. if (!used[i] || Find(used[i]))
  27. {
  28. used[i] = u;
  29. return ;
  30. }
  31. }
  32. }
  33. return ;
  34. }
  35. int main ()
  36. {
  37. while (scanf ("%d %d", &n, &m), n||m)
  38. {
  39. memset (maps, , sizeof(maps));
  40. while (m --)
  41. {
  42. int u, v;
  43. scanf ("%d %d", &u, &v);
  44. maps[u][v] = ;
  45. }
  46. floyd ();
  47. memset (used, , sizeof(used));
  48. int res = ;
  49. for (int i=; i<=n; i++)
  50. {
  51. memset (vis, , sizeof(vis));
  52. res += Find(i);
  53. }
  54. printf ("%d\n", n - res);
  55. }
  56. return ;
  57. }
  1. #include <iostream>//H_K算法
  2. #include <cstring>
  3. #include <queue>
  4. #include <cmath>
  5. #include <cstdio>
  6. using namespace std;
  7.  
  8. const int maxn = ;
  9. const int INF = 0x3f3f3f3f;
  10. int maps[maxn][maxn], n, m, dx[maxn], dy[maxn];
  11. int vis[maxn], cx[maxn], cy[maxn], dis;
  12. void floyd ()
  13. {
  14. for (int k=; k<=n; k++)
  15. for (int i=; i<=n; i++)
  16. for (int j=; j<=n; j++)
  17. if (maps[i][k] && maps[k][j])
  18. maps[i][j] = ;
  19. }
  20. bool bfs ()
  21. {
  22. queue <int> Q;
  23. dis = INF;
  24. memset (dx, -, sizeof(dx));
  25. memset (dy, -, sizeof(dy));
  26. for (int i=; i<=n; i++)
  27. if (cx[i] == -)
  28. {
  29. Q.push(i);
  30. dx[i] = ;
  31. }
  32. while (!Q.empty())
  33. {
  34. int u = Q.front();
  35. Q.pop();
  36. if (dx[u] > dis)
  37. break;
  38. for (int i=; i<=n; i++)
  39. {
  40. if (dy[i]==- && maps[u][i])
  41. {
  42. dy[i] = dx[u] + ;
  43. if (cy[i] == -)
  44. dis = dy[i];
  45. else
  46. {
  47. dx[cy[i]] = dy[i] +;
  48. Q.push (cy[i]);
  49. }
  50. }
  51. }
  52. }
  53. return dis != INF;
  54. }
  55. int dfs (int u)
  56. {
  57. for (int v=; v<=n; v++)
  58. {
  59. if (!vis[v] && dx[u]+==dy[v] && maps[u][v])
  60. {
  61. vis[v] = ;
  62. if (cy[v]!=- && dis==dy[v])
  63. continue;
  64. if (cy[v]==- || dfs(cy[v]))
  65. {
  66. cx[u] = v;
  67. cy[v] = u;
  68. return ;
  69. }
  70. }
  71. }
  72. return ;
  73. }
  74. int Max_match ()
  75. {
  76. int res = ;
  77. memset (cx, -, sizeof(cx));
  78. memset (cy, -, sizeof(cy));
  79. while (bfs())
  80. {
  81. memset (vis, , sizeof(vis));
  82. for (int i=; i<=n; i++)
  83. if (cx[i] == -)
  84. res += dfs (i);
  85. }
  86. return res;
  87. }
  88. int main ()
  89. {
  90. while (scanf ("%d %d", &n, &m), n||m)
  91. {
  92. memset (maps, , sizeof(maps));
  93. while (m --)
  94. {
  95. int u, v;
  96. scanf ("%d %d", &u, &v);
  97. maps[u][v] = ;
  98. }
  99. floyd ();
  100. printf ("%d\n", n - Max_match());
  101. }
  102. return ;
  103. }

Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)的更多相关文章

  1. poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)

    http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total ...

  2. POJ 2594 Treasure Exploration(最小路径覆盖变形)

    POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...

  3. POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

    Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  4. poj 2594 Treasure Exploration (二分匹配)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6558   Accepted: 2 ...

  5. POJ 2594 Treasure Exploration 最小可相交路径覆盖

    最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如 ...

  6. poj 2594 Treasure Exploration 二分图匹配

    点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7215   ...

  7. POJ 2594 Treasure Exploration (可相交最小路径覆盖)

    题意 给你张无环有向图,问至少多少条路径能够覆盖该图的所有顶点--并且,这些路径可以有交叉. 思路 不是裸的最小路径覆盖,正常的最小路径覆盖中两个人走的路径不能有重复的点,而本题可以重复. 当然我们仍 ...

  8. POJ 2594 Treasure Exploration (Floyd+最小路径覆盖)

    <题目链接> 题目大意: 机器人探索宝藏,有N个点,M条边.问你要几个机器人才能遍历所有的点. 解题分析: 刚开始还以为是最小路径覆盖的模板题,但是后面才知道,本题允许一个点经过多次,这与 ...

  9. POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)

    题意:  派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.有的点可以重复去. 输入数据: 首先是n和 ...

随机推荐

  1. eclipse提速02 - eclipse.ini优化

    给eclipse执行jvm.它可以让你使用自己的jdk,而不是系统环境变量所指定的jdk -vm /path/to/your/java 使用最新的jdk来运行eclipse.使用最新的jdk要好很多. ...

  2. nexus-3本地下载jar的settipng.xml配置

    打开maven安装目录下的setting.xml <servers> <server> <id>nexus</id> <username>a ...

  3. 利用Druid实现应用和SQL监控

    一.关于Druid Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource 高效可 ...

  4. [Angular] Architectures for Huge Angular Based Enterprise

    Using Angular CLI v6, we are able to create library or small application inside a Angular CLI genera ...

  5. ubuntu安装nginx时提示error: the HTTP rewrite module requires the PCRE library

    ubuntu安装nginx时提示error: the HTTP rewrite module requires the PCRE library 须要安装pcre包. sudo apt-get upd ...

  6. 微信小程序之 Tabbar(底部选项卡)

    1.项目目录 2.在app.json里填写:tab个数范围2-5个 app.json { "pages": [ "pages/index/index", &qu ...

  7. 在云服务器 ECS Linux CentOS 7 下重启服务不再通过 service 操作,而是通过 systemctl 操作

    在云服务器 ECS Linux CentOS 7 下重启服务不再通过 service  操作,而是通过 systemctl 操作. 操作说明如下: 1. 查看 sshd 服务是否启动: 看到上述信息就 ...

  8. 下载Youku视频观看

    所需工具: 1.chorme内核浏览器,如Chorme.Firefox等等 2.vlc视频播放器 准备工作完成,开始工作 1.打开优酷的随便一个视频 2.按下F12选择Network(网络)选择Med ...

  9. nhibernate的关系

    用nhibernate,觉得比较难把握的是其中表间的关系. 我用的是Fluently Nhibernate,直接用代码,而不是XML来书写代码与数据表的映射.其中表间关系有3种: 1.Referenc ...

  10. create database 默认utf-8

    CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci; 这是sql语句 CREATE TA ...