poj 2594 Treasure Exploration 二分图匹配

点击打开链接题目链接

Treasure Exploration

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 7215   Accepted: 2947

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

某条路径可以重复走多次

用floyed求出可以到达的点

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 555
using namespace std;
int n,m;
bool g[MAXN][MAXN];
bool vis[MAXN];
int link[MAXN];
void init(){
    memset(vis,0,sizeof(vis));
    memset(link,-1,sizeof(link));
    memset(g,0,sizeof(g));
}
void floyed(){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(g[i][j]==0){
                for(int k=0;k<n;k++){
                    if(g[i][k]&&g[k][j]){
                        g[i][j]=1;
                        break;
                    }
                }
            }
        }
    }
}
bool finds(int x){
    for(int i=0;i<n;i++){
        if(!vis[i]&&g[x][i]){
            vis[i]=1;
            if(link[i]==-1||finds(link[i])){
                link[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main(){
    while(scanf("%d %d",&n,&m)&&(n||m)){
        init();
        int st,ed;
        for(int i=1;i<=m;i++){
            scanf("%d %d",&st,&ed);
            st--;
            ed--;
            g[st][ed]=1;
        }
        floyed();
        int ans=0;
        for(int i=0;i<n;i++){
            memset(vis,0,sizeof(vis));
            if(finds(i))
                ans++;
        }
        printf("%d\n",n-ans);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 06:47:23

poj 2594 Treasure Exploration 二分图匹配的相关文章

POJ - 2594 Treasure Exploration 二分图匹配 + floyd

题目大意:在火星上有N个矿,有点矿之间存在着一条路,由于在火星比较特殊,该路变成了单向路,且机器人只能出现在这条路的两个端点,问最少需要派多少机器人,才能探清这些矿 解题思路:路可以拼接起来形成一条新的路,所以在所给的条件下还可以再扩展,用floyd将所有能连通的点找出来 接下来就是二分匹配的过程了,求出最大匹配数,在用n-最大匹配数就是答案了 #include<cstdio> #include<cstring> #include<vector> using names

poj 2594 Treasure Exploration (二分匹配)

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6558   Accepted: 2644 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored

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

POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要在多一步.利用floyd求出传递闭包.然后依据这个新的图去做最小路径覆盖就可以 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using names

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

题目链接: Poj 2594 Treasure Exploration 题目描述: 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过. 解题思路: 一眼看上去是普通的最小边覆盖,但是仔细想后发现如果在原图上进行最大匹配的话,每个点只能经过一次.这样的话对于本题求出的并不是最优解,所以我们要先对原图进行传递闭包处理,然后再进行最大匹配. 这个题目点数太少H_K和匈牙利算法在空间和时间上并没有什么差,就代码复杂度而言匈牙利算法更有优

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

Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2594 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploratio

poj 2594 Treasure Exploration(最小路径覆盖/二分最大匹配)

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2944 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored

poj 2594 Treasure Exploration 最少边覆盖+传递闭包

给出一个有向无环图,问最少多少条边就可以覆盖所有的点,很明显的最少路径覆盖问题,但是点可以重复用,这与hungary算法不太一样,我们可以这样处理,假设a要经过b点到c,但是b点已经被访问过,为了保证a顺利到c,就让a直接飞过b到c,也就是说添加一条a到c的边,这其实就是求传递闭包,我们可以通过floyd算法求出.然后ans=n-hungary(). 一开始用了邻接表的最大二分匹配,在求闭包的时候用的dfs,但是超时了,估计是求闭包的时候加边过多导致的,而且还要判断边是否重复,因此在时空复杂度允

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

最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如果1->2->3->4那么1可以到达2,3,4只要需要借助一些点,那么就可以直接把1与2,3,4相连,这就是floyd要做的事. 证明:为了连通两个点,某条路径可能经过其它路径的中间点.比如1->3->4,2->4->5.但是如果两个点a和b是连通的,只不过中间需要经

poj 2594 Treasure Exploration(最小路径覆盖,可重点)

题意:选出最小路径覆盖图中所有点,路径可以交叉,也就是允许路径有重复的点. 分析:这个题的难点在于如何解决有重复点的问题-方法就是使用Floyd求闭包,就是把间接相连的点直接连上边,然后就是求最小路径覆盖了.我来大概解释一下为什么是对的,首先我们要明确,当我们重复利用一个点的时候,一定是有两个比较良好的路径相交了,而二分图是不允许这样的情况存在的,因为那必然存在了一个点有一个以上的出度或者入度了,而怎么避免这个问题呢,看下面的图: 这就是针对这个问题的一个典型的模型,如果使用正常二分图,求得的匹