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

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.

Output

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

Sample Input

  1. 1 0
  2. 2 1
  3. 1 2
  4. 2 0
  5. 0 0

Sample Output

  1. 1
  2. 1
  3. 2

Source

最小路径覆盖(非最小边覆盖):

使用最少的边把所有点覆盖=原点数-最大匹配(修改后的图)

注意一种情况:

5  4

1-->3

2-->3

3-->4

3-->5

这里其实用2个robots就行了,但未处理过的图得出来的结果却不是,可见这题是要把图处理一下的,我用了floyd,感觉O(n^3)是会TLE的,都是没有。

  1. //1144K 969MS C++ 1024B 2014-06-14 09:27:31
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define N 505
  5. int g[N][N];
  6. int match[N];
  7. int vis[N];
  8. int n,m;
  9. void floyd()
  10. {
  11. for(int k=;k<=n;k++)
  12. for(int i=;i<=n;i++)
  13. for(int j=;j<=n;j++)
  14. if(g[i][k] && g[k][j])
  15. g[i][j]=;
  16. }
  17. int dfs(int u)
  18. {
  19. for(int i=;i<=n;i++)
  20. if(!vis[i] && g[u][i]){
  21. vis[i]=;
  22. if(match[i]==- || dfs(match[i])){
  23. match[i]=u;
  24. return ;
  25. }
  26. }
  27. return ;
  28. }
  29. int hungary()
  30. {
  31. int ret=;
  32. memset(match,-,sizeof(match));
  33. for(int i=;i<=n;i++){
  34. memset(vis,,sizeof(vis));
  35. ret+=dfs(i);
  36. }
  37. return ret;
  38. }
  39. int main(void)
  40. {
  41. int a,b;
  42. while(scanf("%d%d",&n,&m)!=EOF && (n+m))
  43. {
  44. memset(g,,sizeof(g));
  45. for(int i=;i<m;i++){
  46. scanf("%d%d",&a,&b);
  47. g[a][b]=;
  48. }
  49. floyd();
  50. printf("%d\n",n-hungary());
  51. }
  52. return ;
  53. }

poj 2594 Treasure Exploration (二分匹配)的更多相关文章

  1. poj 2594 Treasure Exploration 二分图匹配

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

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

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

  3. Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)

    题目链接: Poj 2594 Treasure Exploration 题目描述: 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过 ...

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

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

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

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

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

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

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

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

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

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

  9. poj 2594 Treasure Exploration(最小路径覆盖,可重点)

    题意:选出最小路径覆盖图中所有点,路径可以交叉,也就是允许路径有重复的点. 分析:这个题的难点在于如何解决有重复点的问题-方法就是使用Floyd求闭包,就是把间接相连的点直接连上边,然后就是求最小路径 ...

随机推荐

  1. fzu1342

    http://acm.fzu.edu.cn/problem.php?pid=1342 dp[i][j]  , i位尾巴为j的概率 const int maxn = 1008 ; double dp[m ...

  2. 2.3 ARM寄存器详解

    一共有37个寄存器 1. 31个通用寄存器 2. 6个状态寄存器 R13作为堆栈指针 R14链接寄存器 1.保存函数返回地址 2. 异常返回地址 R15程序计数器(PC指针) 程序状态寄存器 只有在异 ...

  3. QTP操作excel文档

    对于QTP操作excel的大前提是,保证组建服务里的DCOM配置里存在 microsoft excel application ,具体的查看方式是,在运行框中输入dcomcnfg,然后会打开组件服务的 ...

  4. PHP5.6.15连接Sql Server 2008配置方案

    php5.6的如果想连接Sql Server 2008数据库,需要手动配置扩展和安装一个驱动. 下载SQL Server Driver for PHP的扩展包,64位系统的官方不支持,找到一个非官方的 ...

  5. Github上传自己的工程

    1.注册并新建项目 2.配置github for windows 前题:安装相应的github for windows 2.1 获取密钥 可以用命令的模式(Git bash),参考资料中有相应的用法: ...

  6. JS-安全检测JavaScript基本数据类型和内置对象的方法

    前言:在前端开发中经常会需要用到检测变量数据类型的需求,比如:判断一个变量是否为undefined或者null来进行下一步的操作,今天在阅读“编写高质量代码-改善JavaScript程序的188个建议 ...

  7. Tomcat回收连接

    最近公司一个JDK1.4的老项目升级了JDK1.6后BUG不断,最可恶的连接池被占满. 因为是使用tomcat的连接池所以在config下中添加 <Resource name="jdb ...

  8. 从request获取远程IP地址

    public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("X-F ...

  9. 那些年,我们一起追的面试题。。to be continued!!!

    1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”?答:Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件.Java被 ...

  10. 自己写的 限制文本框TEdit中只能输入数字

    procedure TForm4.Edit1KeyPress(Sender: TObject; var Key: Char); begin , #]) then begin Key := #; end ...