Treasure Exploration
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 7455   Accepted: 3053

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
题解:需要加上floyd,用上vector 竟然re了;
ac代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
int mp[MAXN][MAXN],vis[MAXN],usd[MAXN];
int N;
bool dfs(int u){
for(int i=;i<=N;i++){
if(!vis[i]&&mp[u][i]){
vis[i]=;
if(!usd[i]||dfs(usd[i])){
usd[i]=u;return true;
}
}
}
return false;
}
void floyd(){
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
if(mp[i][j]==){
for(int k=;k<=N;k++)
if(mp[i][k]&&mp[k][j])
mp[i][j]=;
}
}
int main(){
int M;
while(scanf("%d%d",&N,&M),N|M){
int a,b;
mem(mp,);mem(usd,);
while(M--){
scanf("%d%d",&a,&b);
mp[a][b]=;
}
int ans=;
floyd();
for(int i=;i<=N;i++){
mem(vis,);
if(dfs(i))ans++;
}
printf("%d\n",N-ans);
}
return ;
}

re代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
int vis[MAXN],usd[MAXN];
vector<int>vec[MAXN];
int N;
bool dfs(int u){
for(int i=;i<vec[u].size();i++){
int v=vec[u][i];
if(!vis[v]){
vis[v]=;
if(!usd[v]||dfs(usd[v])){
usd[v]=u;return true;
}
}
}
return false;
}
void floyd(){
for(int i=;i<=N;i++){
for(int k=;k<vec[i].size();k++)
for(int j=;j<vec[vec[i][k]].size();j++){
vec[i].push_back(vec[vec[i][k]][j]);
}
}
}
int main(){
int M;
while(scanf("%d%d",&N,&M),N|M){
int a,b;
mem(usd,);
for(int i=;i<=N;i++)vec[i].clear();
while(M--){
scanf("%d%d",&a,&b);
vec[a].push_back(b);
}
floyd();
int ans=;
for(int i=;i<=N;i++){
mem(vis,);
if(dfs(i))ans++;
}
printf("%d\n",N-ans);
}
return ;
}

Treasure Exploration(二分最大匹配+floyd)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ2594 Treasure Exploration(最小路径覆盖)

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

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

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

  8. Treasure Exploration POJ - 2594 【有向图路径可相交的最小路径覆盖】模板题

    Have you ever read any book about treasure exploration? Have you ever see any film about treasure ex ...

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

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

随机推荐

  1. SQL Server 2008 错误 233 的解决办法

    问题一.忘记了登录Microsoft SQL Server 2008 的sa的登录密码 解决方法:先用windows身份验证的方式登录进去,然后在‘安全性’-‘登录’-右键单击‘sa’-‘属性’,修改 ...

  2. 初探ListView

    ListView可能是Android开发中最常用的一个控件,但要用的纯熟还需要不断的锻炼. 建立简单的ListView 1.在布局文件(.xml)中添加<ListView>标签 2.在Ma ...

  3. js页面跳转 和 js打开新窗口 方法

    js页面跳转 和 js打开新窗口 方法 第一种: 第二种: 第三种: 第四种: 第五种: 1.在原来的窗体中直接跳转用 window.location.href="你所要跳转的页面" ...

  4. 几个linux 下C/C++集成开发环境推荐

    链接地址:http://www.lupaworld.com/article-210675-1.html 摘要: 一.AnjutaAnjuta是一个多语言的IDE,它最大的特色是灵活,同时打开多个文件, ...

  5. 在C#调用C++的DLL方法(二)生成托管的DLL

    写操作之前,还是扼要的说一下托管与非托管C++的区别好了,其实我也并没有深入了解过托管C++的特点所在,其最大的特征就是可以由系统来调试回收相关的代码资源,跟C#的特性一样,只是编程风格跟C++类似而 ...

  6. MyEclipse 在loading workbench 启动卡死

    解决方法: 找到Workspace目录,在.metadata(Mac 下是在 .metadata/.plugins)中删掉以下两个文件 org.eclipse.ui.workbench org.ecl ...

  7. Altium designer 10如何设置标题栏

    一.修改设置 1.执行Design-Document Option,打开文档属性对话框,设置其中title等参数. 2.执行Place-Text String,按TAB键,将Text属性中设置为&qu ...

  8. Java并发编程总结2——慎用CAS(转)

    一.CAS和synchronized适用场景 1.对于资源竞争较少的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源:而CAS基于硬件实 ...

  9. WEB和APP谁是互联网未来

    据中国多家权威报告显示,作为多年专业化互联网公司炎帝网络科技综合评估预测,预计2016年全球互联网设备将达到100亿部.如果届时全球人口达到73亿,意味着平均每人将有1.4部设备.智能交通将增长50倍 ...

  10. BZOJ 1874 取石子游戏 (NIM游戏)

    题解:简单的NIM游戏,直接计算SG函数,至于找先手策略则按字典序异或掉,去除石子后再异或判断,若可行则直接输出. #include <cstdio> const int N=1005; ...