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. MySql面试题、知识汇总、牛客网SQL专题练习

    点击名字直接跳转到链接: Linux运维必会的100道MySql面试题之(一) Linux运维必会的100道MySql面试题之(二) Linux运维必会的100道MySql面试题之(三) Linux运 ...

  2. Java压缩字符串工具类

    StringCompressUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.By ...

  3. java基础—方法重载(overload)

    一.方法的重载 方法名一样,但参数不一样,这就是重载(overload). 所谓的参数不一样,主要有两点:第一是参数的个数不一样,第二是参数的类型不一样.只要这两方面有其中的一方面不一样就可以构成方法 ...

  4. Servlet的引入(即加入Servlet)

    今天讲的Servlet是根据上一章节<创建一个学生信息表,与页面分离>而结合. 一.看图分析 此模式有问题: 1.jsp需要呼叫javabean StudentService stuSer ...

  5. Java第11次作业:什么是继承?继承的好处?什么是覆写?super()?构造代码块?子父类初始化顺序? 抽象类能用final声明吗?final关键字声明类 方法 变量以及全局常量?抽象类的构造方法?

    什么是继承? 继承是以父类为基础,子类可以增加新的数据或新的功能.子类不能选择性地继承父类.这种技术使得复用以前的代码非常容易. JAVA不支持多继承,单继承使JAVA的继承关系很简单,一个类只能有一 ...

  6. stataic 变量

    static 是静态变量的的类型说明符 静态变量属于静态存储方式,(外部变量也是静态存储方式) 静态的局部变量 静态局部变量属于静态存储方式,它具有以下特点: (1)静态局部变量在函数内定义 它的生存 ...

  7. 201621123080《Java程序设计》第十一周学习总结

    201621123080<Java程序设计>第十一周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 ...

  8. 【mysql】【windows】MySQL 服务无法启动,服务没有报告任何错误,请键入 NET HELPMSG 3534 以获得更多的帮助。

    成功安装以后,启动MySQL,输入: net start mysql 提示: ”MySQL 服务无法启动,服务没有报告任何错误,请键入 NET HELPMSG 3534 以获得更多的帮助.” 查了下, ...

  9. 请问batch_normalization做了normalization后为什么要变回来?

    请问batch_normalization做了normalization后为什么要变回来? 请问batch_normalization做了normalization后为什么要变回来? - 莫驚蟄的回答 ...

  10. selection problem-divide and conquer

    思路: 随机选取列表中的一个值v,然后将列表分为小于v的,等于v的,大于v的三组.对于k<=left.size()时, 在left中执行selection:落在中间的,返回v:k>left ...