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

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

————————————————————————————

一个有向图中, 有若干条连接的路线, 问最少放多少个机器人,可以将整个图上的点都走过。 最小路径覆盖问题。

思路:最小路径覆盖问题, 最小路径覆盖 = |V| - 最大匹配数。 当然做这道题还有一个坑!! 如果有向图的边有相交的情况,那么就不能简单的对原图求二分匹配了,建图时dfs预处理将每个点能到达的点都与他连起来

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
const int MAXN=1005;
int uN,vN,n; //u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int link[MAXN];
int vis[MAXN];
bool dfs(int u)
{
int v;
for(v=1; v<=vN; v++)
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
return false;
} int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=1; u<=uN; u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
} void dfs2(int pos,int x)
{
for(int i=0; i<n; i++)
{
if(g[pos][i]==1&&!vis[i])
{
vis[i]=1;
g[x][i]=1;
dfs2(i,x);
}
}
} int main()
{
int m,k,x,y,T;
while(~scanf("%d%d",&n,&m)&&(m||n))
{ memset(g,0,sizeof g);
for(int i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
g[x][y]=1;
}
for(int i=1; i<=n; i++)
{
memset(vis,0,sizeof vis);
vis[i]=1;
dfs2(i,i);
}
uN=vN=n;
printf("%d\n",n-hungary());
}
return 0;
}

  

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

  1. POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包

    题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 655 ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. POJ Treasure Exploration 【DAG交叉最小路径覆盖】

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

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

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

随机推荐

  1. 17.Mysql分区

    17.Mysql分区分区是指根据一定的规则把一个表分解成多个部分,逻辑上仍是一张表,实际上由多个物理分区对象组成.分区对于应用是完全透明的,不影响业务逻辑和SQL编写.分区的优点: 可以存储更多的数据 ...

  2. nvl 与 nvl2

    NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...

  3. istio-jaeger-spring boot调用链配置

    istio-jaeger-spring boot调用链配置 虽然,istio ingress controller已经生成了jaeger 记录所需要的信息,但是多个分布式之间没法清晰记录相互之间的依赖 ...

  4. Redis单机安装

    安装过程 安装环境 ubuntu14.04 server wget http://download.redis.io/releases/redis-3.2.5.tar.gz tar -xzvf ./r ...

  5. phpstorm+xdebug调试代码

    1工具 #phpstorm 前面有文章介绍如何安装 #phpStudy 官网下的2018最新的安装包,php环境使用的也是最新的php7.0nts 2开启php Xdebug拓展 开启拓展,phpSt ...

  6. Ant.OutputIsUnreadableCode

    Ant在Mac OS X终端中的输出乱码的问题 1. 问题: 在用Ant脚本进行构建Android App时,在编译失败时,Ant 输出有乱码. 2. 环境: Mac OS X, 简体中文版.在Ter ...

  7. spring配置遇到的问题

    1.文档根元素 "beans" 必须匹配 DOCTYPE 根 "null" 这个原因是因为我自动扫描mapping.xml的文件路径设置错误,把它设置成spri ...

  8. 需求文件requirements.txt的创建及使用

    pip freeze >requirements.txt pip install -r requirements.txt

  9. windows driver

    C:\Windows\System32\DriverStore\FileRepository

  10. 工具类官网Web原型制作分享-Adobe

    Adobe是全球知名的软件开发团队,研发了设计创意领域全球领先的优秀软件产品,为设计行业提供了巨大的价值. 网站原型以图文排版为主,顶部一级导航,弹出面板和面板的使用实现了一级导航下拉的效果. 本原型 ...