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

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

题意:寻宝,有N个点,m条路,接下来m行每行两个数a,b表示a到b之间连有一条单向路,所以此题图为有向图。每个机器人可以从任意点作为起点然后到达终点,但无法返回,所以求最少需要多少机器人走完这n个点。

思路:很明显最小路径覆盖问题,最小路径覆盖=顶点数-最大点覆盖;但这题的路有点特殊,为了节约成本,当然是只要有路能走下去就一直走下去,题目表明可以重复经过某个点。所以非直接相连的点我们用floyd将其连接,在求连接后的图的最小路径覆盖。这便是此题的思路;

const int N=500+10;
int n,m,w[N][N],linked[N],v[N];
void floyd()//传递闭包
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(!w[i][j]&&w[i][k]&&w[k][j])
w[i][j]=1;
// for(int k=1; k<=n; k++)
// for(int i=1; i<=n; i++)
// printf("%d %d %d\n",k,i,w[k][i]);
}
int dfs(int u)
{
for(int i=1;i<=n;i++)
if(w[u][i]&&!v[i])
{
v[i]=1;
if(linked[i]==-1||dfs(linked[i]))
{
linked[i]=u;
return 1;
}
}
return 0;
}
void hungary()
{
int ans=0;
memset(linked,-1,sizeof(linked));
for(int i=1;i<=n;i++)
{
memset(v,0,sizeof(v));
if(dfs(i)) ans++;
}
printf("%d\n",n-ans);//最小路径覆盖;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
int u,v;
memset(w,0,sizeof(w));
while(m--)
{
scanf("%d%d",&u,&v);
w[u][v]=1;
}
floyd();
hungary();
}
return 0;
}

妙哉!

POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. poj 2594 Treasure Exploration (二分匹配)

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

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

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

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

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

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

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

随机推荐

  1. Appium教程---Client/Server Architecture

    appium的核心其实是一个暴露了一系列REST API的server. 这个server的功能其实很简单:监听一个端口,然后接收由client发送来的command.翻译这些command,把这些c ...

  2. ssrs 2016, mobile report error: The report may be misconfigured, the data may not be available, or the server version may be unsupported.

    使用多账户配置ssrs mobile report 权限后,使用客户端显示: 使用web 查阅,显示: 遇到这种情况,可能是由于,report引用了 数据集文件夹中的数据集,请记得把数据集文件夹上为该 ...

  3. js连续赋值,你理解了吗

    看一道有意思的题,也许你会自信满满地写下答案,会是正确的吗? }; var b = a; a.x = a = {n: }; console.log('a',a); console.log('b',b) ...

  4. 214 Shortest Palindrome 最短回文串

    给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...

  5. 工作记录 angular页面操作 MD5加密

    今天只是做页面,基于angularjs,有美工做的图打底,确实好用 密码保存,用到了C# MD5加密: https://www.cnblogs.com/healer007/p/5062189.html

  6. git 学习笔记1

    目前我属于粗放型的[学习者],接下来需要做一些改变,让自己更加规范.首先需要学习的就是版本控制系统,本科在工作室的时候使用过一点Subversion,不过到现在已经基本没有印象了.git现在越来越成为 ...

  7. Petri网的工具

    需要寻找 Petri 网的工具的朋友可以在 http://www.informatik.uni-hamburg.de/TGI/PetriNets/tools/complete_db.html 里面找一 ...

  8. (转) 淘淘商城系列——CMS内容管理系统工程搭建

    http://blog.csdn.net/yerenyuan_pku/article/details/72825801 淘淘商城系列——CMS内容管理系统工程搭建 上文我们一起搭建了表现层中的商城门户 ...

  9. Which dispatch method would be used in Swift?-Existential Container

    In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { ...

  10. C#委托与事件的关系(转载)

    1.C#中的事件和委托的作用?事件代表一个组件能够被关注的一种信号,比如你的大肠会向你发出想拉屎的信号,你就可以接收到上厕所.委托是可以把一个过程封装成变量进行传递并且执行的对象,比如你上蹲坑和上坐马 ...