传送门:http://poj.org/problem?id=2594

Treasure Exploration
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 9802   Accepted: 3979

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

Source

题意概括:

给一个 N个节点 M 条边的有向图,机器人会沿着路径前进,问最少放多少机器人可以把所有的点走完。

解题思路:

如果只是按普通的最小路径覆盖会出现问题,两个可以通过一个结点(或多个)可以相连的结点 有可能不被匹配到,导致路径数不是最少的。原因就在于路径可以交叉。

所以先Floyd跑一遍求出原图的传递闭包,再跑最小路径覆盖就能解决上面的问题了。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = ;
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int N, M; bool Find(int x)
{
for(int i = ; i <= N; i++){
if(!used[i] && g[x][i]){
used[i] = true;
if(linker[i] == - || Find(linker[i])){
linker[i] = x;
return true;
}
}
}
return false;
}
void Floyd()
{
for(int i = ; i <= N; i++){
for(int j = ; j <= N; j++){
for(int k = ; k <= N; k++){
if(g[i][k] == && g[k][j] == ) g[i][j] = ;
}
}
}
} void init()
{
memset(g, ,sizeof(g));
memset(linker, -, sizeof(linker));
}
int main()
{
int u, v;
while(~scanf("%d%d", &N, &M) && (N+M)){
init();
while(M--){
scanf("%d%d", &u, &v);
g[u][v] = ;
}
Floyd();
int ans = ;
for(int i = ; i <= N; i++){
memset(used, , sizeof(used));
if(Find(i)) ans++;
}
printf("%d\n", N-ans);
}
return ;
}

POJ Treasure Exploration 【DAG交叉最小路径覆盖】的更多相关文章

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

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

  2. POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)

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

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

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

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

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

  5. UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32568 [思路] DAG的最小路径覆盖. 将每个人看做一个结点,如 ...

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

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

  7. poj 3020 Antenna Placement(最小路径覆盖 + 构图)

    http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  8. poj 2060 Taxi Cab Scheme (最小路径覆盖)

    http://poj.org/problem?id=2060 Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submi ...

  9. POJ 3216 Repairing Company(最小路径覆盖)

    POJ 3216 Repairing Company id=3216">题目链接 题意:有m项任务,每项任务的起始时间,持续时间,和它所在的block已知,且往返每对相邻block之间 ...

随机推荐

  1. Map接口常用实现类学习

    HashMap 1.6的HashMap:数组加单向链表结构 最重要的内部类Entry,全类名是java.util.HashMap.Entry,是个静态类,实现了Map.Entry接口.HashMap. ...

  2. Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.xService }: app is in background uid UidRecord(一)

    Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.x ...

  3. flask综合整理1

    前言: 框架的对比 Django:1个重武器,包含了web开发中常用的功能.组件的框架:(ORM.Session.Form.Admin.分页.中间件.信号.缓存.ContenType....): To ...

  4. 图解DTS和PTS

    由于把视频编码成I,B,P等帧,如下图   假设现在有I,B,P帧,那么要传输和显示呢??   如果按照显示顺序传输的话: 传输顺序就是I->B>P 当对B帧进行解码后,由于B帧无法单独显 ...

  5. 024-cxf.xml配置文件模板

    版本一 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...

  6. Django(5) session登录注销、csrf及中间件自定义、django Form表单验证(非常好用)

    一.Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 数据库(默认) 缓存 文件 缓存+数据库 加密cookie 1.数据库Session 1 2 3 4 5 ...

  7. WebAssembly简单指导---译

    开发者指导 本页面提供一步一步的操作将一个简单的程序编译成webassembly 前提要求 为了编译成webAssembly,需要提前安装一些工具: Git.在Linux和OSX下已自带了Git,在W ...

  8. Remove a Submodule within git

    For many git-based projects, submodules are useful in avoiding duplicate work and easing utility lib ...

  9. MVC Request.UrlReferrer为null

    使用情景,登录后返回登录前访问的页面. 这个时候用到了UrlReferrer var returnUrl = HttpContext.Current.Request.UrlReferrer != nu ...

  10. 学习总结(一)java web连接池

    大家都知道,在访问数据库时要与数据库建立连接.在jdbc中,用户与数据库建立连接后,取完数据或操作完数据后,就会断开这个连接.当下次还要访问数据库时,就会重新创建连接.这样很浪费资源,尤其是网页上有数 ...