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. js call方法的使用

    转自:js call call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg ...

  2. iOS.C

    iOS开发中C语言的应用: 1. NS_ENUM & NS_OPTIONS http://nshipster.com/ns_enum-ns_options/

  3. pop回到之前的某一个页面

    循环遍历 - (void)backHome:(UIButton *)button { self.navigationController.navigationBarHidden = NO; 4 Cas ...

  4. javascript 事件流的应用之 addEventListener

    原始需求:防止按钮短时间内高频率触发点击事件,由于重复提交导致的业务异常. 图: demo: <!DOCTYPE html> <html lang="en" di ...

  5. easyui下拉框过滤优化

    项目中有个需求:编辑combobox的输入域会自动检索匹配项,当没有任何匹配项时,将combobox重置为初始状态. 处理方式:重写输入域的blur事件,判断当前值是否为加载的数据集的子集,如果不是则 ...

  6. Ajax(6) Ajax向servlet请求数据库操作 并显示到当前页面 这个未经测试

    假设:1.你的页面在Web-Root下,内容为: <div id="showMsg"></div><input type="text&quo ...

  7. 20155312 2006-2007-2 《Java程序设计》第二周学习总结

    20155312 2006-2007-2 <Java程序设计>第二周学习总结 课堂内容总结 git:版本控制 生活中的容灾备份 归纳思维.实验思维.计算思维 计算机:实现自动化 学会使用快 ...

  8. BP神经网络在python下的自主搭建梳理

    本实验使用mnist数据集完成手写数字识别的测试.识别正确率认为是95% 完整代码如下: #!/usr/bin/env python # coding: utf-8 # In[1]: import n ...

  9. 正则表达式,re模块

    一.正则表达式 正则表达式 : 匹配字符串,一般用于爬取数据. 正则表达式查询网址 : http://tool.chinaz.com/regex/?qq-pf-to=pcqq.group 1.元字符( ...

  10. 谈互联网开放平台:“去中心化”大势所趋 zz

    文/磐石之心 几天前与好友聊到众筹咖啡馆的事情,他向我讲述了一个非常具有特色的众筹咖啡馆案例.而这个案例也引发我对当前互联网开放.去中心和集权的一些思考,今天就简单写出来与大家分享. 一个无赚钱目的的 ...