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. zoj 2229 Ride to School

    所有车子到达的总时间算出来,然后从小到大排序,如果:1. 开始时间 < 0 的,不予考虑,太快的赶不上,太慢的赶上也没用.2. 开始时间 > 0 的,Charley 和最早到达的车子一起到 ...

  2. 防止 XSS 攻击 解决方案

    XSS又叫CSS英文缩写为Cross Site Script中文意思为跨站脚本攻击具体内容指的是恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执 ...

  3. UVa 12299 RMQ with Shifts(线段树)

    线段树,没了.. ----------------------------------------------------------------------------------------- # ...

  4. 5.4.1 RegExp实例属性

    RegExp的每个实例都具有下列属性,通过这些属性可以取得有关模式的各种信息.        1.global:布尔值,表示是否设置了 g 标志.        2.ignoreCase:布尔值,表示 ...

  5. java 实现 一个账号只能在一个地方登陆,其他地方被下线

    其实方法有很多的,我这献丑了. 使用理解java 四大作用域. 思路:理解java 四大作用域的关键. 第一个地方登陆: 1.得到请求的SessionId 和 登陆的 用户名 2.把SessionId ...

  6. 加密PHP文件的方式,目测这样可以写个DLL来加密了

    <?php function encode_file_contents($filename) { $type=strtolower(substr(strrchr($filename,'.'),1 ...

  7. sqlite详细介绍

    ------------------------------------------------------------------------------SQLite简介-------------- ...

  8. AssetManager中的路径参数不能包含"assets/"

    String path = “music/bg.mp3”: //正确的参数 //String path = “assets/music/bg.mp3”: //错误的参数 //String path = ...

  9. Django里面的RequestContext

    c = RequestContext(request, { 'foo': 'bar', }) get_template('about.html').render(c) 当我们定义一个RequestCo ...

  10. Hibernate 知识点复习

    核心接口 1  Configuration接口负责配置并启动Hibernate,创建SessionFactory对象 2  SessionFactory接口负责初始化Hibernate.它充当数据存储 ...