Treasure Exploration

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 9794   Accepted: 3975

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

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 0
2 1
1 2
2 0
0 0

Sample Output:

1
1
2

题意

问有向图里,需要最少多少个机器人,能走遍所有的点,并且可以走其它机器人已经走过的路径。

题解:

这道题我一开始以为就是最小路径覆盖,结果一直WA...

但其实这题和最小路径覆盖有区别,最小路径覆盖的要求就是在图中找尽量少的路径,让每个节点恰好在一条路径上,单独的一个结点也可以作为一条路径。但是,正是因为每个结点在一条路径上,所以没有重复的路径。

但是这题路径是可以重复的,所以可以利用floyd传递闭包,让原来被“阻断”的路径可以链接起来。

推荐一下这篇博客,挺好的,讲的挺详细的:https://www.cnblogs.com/justPassBy/p/5369930.html

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mem(x) memset(x,0,sizeof(x))
using namespace std; const int N = ;
int map[N][N],check[N<<],match[N<<],link[N][N<<];
int n,m,ans; inline int dfs(int x){
for(int i=n+;i<=*n;i++){
if(link[x][i] && !check[i]){
check[i]=;
if(!match[i] || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
} int main(){
while(scanf("%d%d",&n,&m)){
if(!n && !m) break ;
mem(map);mem(match);mem(link);ans=;
for(int i=,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
map[x][y]=;
}
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
if(map[i][k])
for(int j=;j<=n;j++){
if(map[k][j]) map[i][j]=;
}
}
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(map[i][j]) link[i][j+n]=;
for(int i=;i<=n;i++){
mem(check);
if(dfs(i)) ans++;
}
printf("%d\n",n-ans);
}
return ;
}

POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)的更多相关文章

  1. POJ2594 Treasure Exploration(最小路径覆盖)

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

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

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

  3. [POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)

    传送门 引子: 有一个问题,是对于一个图上的所有点,用不相交的路径把他们覆盖,使得每个点有且仅属于一条路径,且这个路径数量尽量小. 对于这个问题可以把直接有边相连的两点 x —> y,建一个二分 ...

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

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

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

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

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

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

  7. POJ-2594 Treasure Exploration,floyd+最小路径覆盖!

                                                 Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...

  8. POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!

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

  9. Treasure Exploration---poj2594(传递闭包Floyd+最小路径覆盖)

    题目链接:http://poj.org/problem?id=2594 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过(这就是和单 ...

随机推荐

  1. Apache Struts最新漏洞 远程代码执行漏洞预警 2018年11月08日

    2018年11月8日,SINE安全监控检测中心,检测到Apache Struts官方更新了一个Struts漏洞补丁,这个漏洞是Apache Struts目前最新的漏洞,影响范围较广,低于Apache ...

  2. Java学习笔记五:Java中常用的运算符

    Java中常用的运算符 运算符是一种“功能”符号,用以通知 Java 进行相关的运算.譬如,我们需要将变量 score 的值设置为 20 ,这时候就需要一个“=”,告诉程序需要进行赋值操作. Java ...

  3. scrapy编写爬虫的时候出现缺少win32api

    环境:python3.6 工具:pycharm2017.3 scrapy fetch http://www.baidu.com ModuleNotFoundError: No module named ...

  4. shell重温---基础篇(参数传递&echo命令)

    经过前两天的学习,关于shell的基础算是知道的一般般啦,最起码不算是小白了(纯属意淫).今天就来点干货哈.   首先是运行shell脚本时的参数传递.脚本内获取参数的格式为$n.n代表了一个数字,例 ...

  5. 【Consul】多数据中心

    Consul的一个关键特性是支持多数据中心.consul架构中提到是构建低耦合的多个数据中心,一个数据中心的网络连接问题或故障不在其他数据中心的可用性.每个数据中心都是独立运行,并且拥有私有的LAN ...

  6. hive报错:Caused by: ERROR XBM0H: Directory /var/lib/hive/metastore/metastore_db cannot be created.

    在cdh集群中,删除之前的hive服务,然后将hive添加到其他节点,然后再通过hive客户端连接hive报错: Caused by: ERROR XJ041: Failed to create da ...

  7. [转]使用Gradle管理你的Android Studio工程

    本文转自:http://www.flysnow.org/2015/03/30/manage-your-android-project-with-gradle.html Gradle简介 Gradle  ...

  8. 在测试时用到的一些mysql的小技巧(持续更新)

    经常使用的快捷键: 1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句 3.ctrl+shift +/ 解除注释 4.ctrl+r 运行查询窗口的sql语句 5.ctrl+shift+r ...

  9. Lua工具类

    1.打印table --一个用以打印table的函数 function print_r (t, name) print(pr(t,name)) end function pr (t, name, in ...

  10. Spring实战第五章学习笔记————构建Spring Web应用程序

    Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...