题目链接:http://poj.org/problem?id=2594

Treasure Exploration
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions:10480   Accepted: 4250

Description

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?

Input

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.
题目大意:给出一张有向图,求最小多少个机器人可以经过所有的点。
思路:
1.因为是可重复经过点的,所以路径是可相交的。求最小路径覆盖 = 原图顶点数 - 新图最大匹配数。
2.对于可相交的最小路径覆盖问题,要先用 floyd 求一下传递闭包。转化为不可相交的最小路径覆盖来求。
代码如下:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define mem(a, b) memset(a, b, sizeof(a))
  4. const int MAXN = ;
  5. const int MAXM = ;
  6.  
  7. int n, m; //n个点 m条有向边
  8. int line[MAXN][MAXN], used[MAXN], master[MAXN];
  9.  
  10. void floyd()
  11. {
  12. for(int i = ; i <= n; i ++)
  13. for(int j = ; j <= n; j ++)
  14. for(int k = ; k <= n; k ++)
  15. if(line[i][k] && line[k][j])
  16. line[i][j] = ;
  17. }
  18.  
  19. int find(int x)
  20. {
  21. for(int i = ; i <= n; i ++) //y部的点
  22. {
  23. if(line[x][i] && used[i] == -)
  24. {
  25. used[i] = ;
  26. if(master[i] == - || find(master[i]))
  27. {
  28. master[i] = x;
  29. return ;
  30. }
  31. }
  32. }
  33. return ;
  34. }
  35.  
  36. int main()
  37. {
  38. while(scanf("%d%d", &n, &m) != EOF)
  39. {
  40. if(n == && m == )
  41. break;
  42. int cnt = ;
  43. mem(line, ), mem(master, -);
  44. for(int i = ; i <= m; i ++)
  45. {
  46. int a, b;
  47. scanf("%d%d", &a, &b);
  48. line[a][b] = ;
  49. }
  50. floyd();
  51. for(int i = ; i <= n; i ++) //x部的点
  52. {
  53. mem(used, -);
  54. if(find(i))
  55. cnt ++; //最大匹配数
  56. }
  57. printf("%d\n", n - cnt);
  58. }
  59. return ;
  60. }

POJ2594 Treasure Exploration【DAG有向图可相交的最小路径覆盖】的更多相关文章

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

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

  2. loj 1429(可相交的最小路径覆盖)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1429 思路:这道题还是比较麻烦的,对于求有向图的可相交的最小路径覆盖,首先要解决成环问 ...

  3. POJ2594 Treasure Exploration[DAG的最小可相交路径覆盖]

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8301   Accepted: 3 ...

  4. Treasure Exploration POJ - 2594 【有向图路径可相交的最小路径覆盖】模板题

    Have you ever read any book about treasure exploration? Have you ever see any film about treasure ex ...

  5. poj 2594(可相交的最小路径覆盖)

    题目链接:http://poj.org/problem?id=2594 思路:本来求最小路径覆盖是不能相交的,那么对于那些本来就可达的点怎么处理,我们可以求一次传递闭包,相当于是加边,这样我们就可以来 ...

  6. poj 2594Treasure Exploration(有向图路径可相交的最小路径覆盖)

    1 #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> ...

  7. HDU 4606 Occupy Cities ★(线段相交+二分+Floyd+最小路径覆盖)

    题意 有n个城市,m个边界线,p名士兵.现在士兵要按一定顺序攻占城市,但从一个城市到另一个城市的过程中不能穿过边界线.士兵有一个容量为K的背包装粮食,士兵到达一个城市可以选择攻占城市或者只是路过,如果 ...

  8. 【LA3126 训练指南】出租车 【DAG最小路径覆盖】

    题意 你在一座城市里负责一个大型活动的接待工作.明天将有m位客人从城市的不同的位置出发,到达他们各自的目的地.已知每个人的出发时间,出发地点和目的地.你的任务是用尽量少的出租车送他们,使得每次出租车接 ...

  9. Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配

    /** 题目:Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配 链接:https://vjudge.net/proble ...

随机推荐

  1. vue编辑、新增弹框(引用外部页面)

    vue编辑.新增弹框(引用外部页面) 2018年06月15日 09:37:20 会飞的猪biubiu 阅读数 10265    版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...

  2. history API,判断页面是否是在跳转链接后返回

    https://www.cnblogs.com/accordion/p/5699372.html history.replaceState(history.state, null, "htt ...

  3. 敌兵布阵(HDU 1166)

    Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...

  4. 1632:【 例 2】[NOIP2012]同余方程

    #include<bits/stdc++.h> #define ll long long using namespace std; void Exgcd(ll a,ll b,ll & ...

  5. 2Dot grammar

    http://www.cnblogs.com/mjios/archive/2013/04/08/3006577.html . #import <Foundation/Foundation.h&g ...

  6. 百度翻译api初使用(很久没写python了,写几行玩玩)

    调用free api做做简易的翻译 这个是百度翻译api文档 http://api.fanyi.baidu.com/api/trans/product/apidoc 照着百度api给的文档向web服务 ...

  7. servlet 中session的使用方法(创建,使用)

    创建: protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, ...

  8. 查看Oracle表空间使用情况

    1.查询表空间的总容量 as MB from sys.dba_data_files a group by a.TABLESPACE_NAME 2.查询表空间的空闲容量 select b.TABLESP ...

  9. docker部署nginx+vue项目

    1.vue项目打包 npm run build 会在项目生成dist文件夹,这个文件夹可以使用nginx或tomcat来发布服务 2.查找nginx基础镜像 可以通过以下网站找到符合自己的基础镜像,我 ...

  10. JAVA基础知识|异常

    一.基础知识 处理异常,java提供了一个优秀的解决方案:异常处理机制. java把异常当作对象来处理,所有的异常都是由Throwable继承而来,但在下一层立即分解为两个分支:Error和Excep ...