Treasure Exploration

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 题解:题意就是让机器人走完所有的路,有向边,问最少需要放几个机器人。
这个很明显是二分图的最小路径覆盖,求一个传递闭包,这个复杂度十分大
至少也是N^3级别了,然后就是节点数-最大匹配数就ok了。
 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
const int N=,M=; int n,m;
int cnt,head[N],next[N*N],rea[N*N];
int map[N][N],flag[N],ly[N]; void add(int u,int v){next[++cnt]=head[u],head[u]=cnt,rea[cnt]=v;}
bool dfs(int u)
{
for (int i=head[u];i!=-;i=next[i])
{
int v=rea[i];
if (flag[v]) continue;
flag[v]=true;
if (!ly[v]||(dfs(ly[v])))
{
ly[v]=u;
return ;
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m)&&(n|m))
{
cnt=;
memset(head,-,sizeof(head));
memset(map,,sizeof(map));
int x,y;
for (int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
map[x][y]=;
}
for (int k=;k<=n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (map[i][k]&&map[k][j]) map[i][j]=;
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (map[i][j]) add(i,j);
memset(ly,,sizeof(ly));
int ans=;
for (int i=;i<=n;i++)
{
memset(flag,,sizeof(flag));
ans+=dfs(i);
}
ans=n-ans;
printf("%d\n",ans);
}
}

非要搞个前向星,然后空间还开小了。

												

POJ2594Treasure Exploration(最小路径覆盖,相交)的更多相关文章

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

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

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

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

  3. POJ2594 Treasure Exploration【DAG有向图可相交的最小路径覆盖】

    题目链接:http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K T ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Windows 漏洞利用开发

    第一阶段:简单栈溢出 分析栈溢出原理 寻找溢出点,了解pattern_create和pattern_offset计算溢出点的原理 寻找JMP ESP跳板,分析利用JMP ESP跳板劫持程序流的原理 编 ...

  2. Sublime +Markdown+OmniMarkupPreviewer 搭建实时预览的markdown编辑器

    浏览器实时预览 <meta http-equiv="refresh" content="0.1"> auto save 的配置 {"aut ...

  3. 从 Objective-C 里的 Alloc 和 AllocWithZone 谈起

    一.问题起源 一切起源于Apple官方文档里面关于单例(Singleton)的示范代码:Creating a Singleton Instance.主要的争议集中在下面这一段: static MyGi ...

  4. C#:CodeSmith根据数据库中的表创建C#数据模型Model + 因为没有钱买正版,所以附加自己写的小代码

    对于C#面向对象的思想,我们习惯于将数据库中的表创建对应的数据模型: 但假如数据表很多时,我们手动增加模型类会显得很浪费时间: 这个时候有些人会用微软提供的EntityFrameWork,这个框架很强 ...

  5. Bzoj 近期题目一句话题解

    目录 Bzoj 近期题目题解 1000: A+B Problem (模拟) 1008: [HNOI2008]越狱 (容斥) 1012: [JSOI2008]最大数maxnumber (线段树) 103 ...

  6. 【启发式拆分】bzoj5200: [NWERC2017]Factor-Free Tree

    和bzoj4059: [Cerc2012]Non-boring sequences非常相似 Description 一棵Factor-Free Tree是指一棵有根二叉树,每个点包含一个正整数权值,且 ...

  7. VS自学日记整理

    vs渣渣自学之旅 一.vs实用插件 二.制作简历之旅 1.一堆错误示范示范 2.标签的使用 3.文件的文本的样式的保存 二.美化博客园之旅 1.第一天 学python有点多这个慢慢消化

  8. python数据类型之字符串(str)和其常用方法

    字符串是有序的,不可变的. 下面的例子说明了字符串是不可变的 name = 'alex' name = 'Jack' """ 并没有变,只是给name开启了一块新内存,储 ...

  9. arm页表在linux中的融合

    参考:arm-linux内存页表创建 arm的第一级页表条目数为4096个,对于4K页第二级目录条目个数为256个,一级二级条目都是每个条目4字节. 在linux下二级分页如下:虚拟地址——> ...

  10. 蓝牙nrf52832的架构和开发(转载)

    相比TI的CC254X.DIALOG的DA1458X,nordic推出的nrf51822和nrf52832在架构和开发商都有自己独特的地方.这几颗产品都是蓝牙低功耗芯片.DA1458X使用OTP硬件架 ...