Treasure Exploration 最小可交叉路径覆盖

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

给出一个单向图,最少可以在几个点上放机器人,能让机器人沿着边把图中所有的节点都访问一遍。其中节点可以重复访问。
这是一个求最小路径覆盖的问题,属于路径可交叉的类型。在路径不可交叉的时候,不需要对原图处理,直接计算最大匹配即可,因为每个节点最多只能访问一次,所以当一个点出现在一条路径中,这个点就不能出现在另一条路径中。但是在路径可交叉的时候,每个节点不止可以访问一次,所以一个点可以出现在多条路径中,但是不对原图处理的匹配算法是通过拆除原匹配寻找新匹配来计算最大匹配的,在可交叉的情况下,原匹配所代表的路径 和 新的匹配代表的路径 均在最小路径覆盖中,所以不对原图进行处理,直接计算最大匹配是不对的。
所以在计算最大匹配之前,要先计算节点所有的可到达节点(直接相连 || 间接相连),在计算图的二分匹配。
而为什么预处理所有可到达的节点就可以了呢?现在仍假设有 n 个独立节点,这个图的最小路径覆盖为 n 。再向这个图里加边,每一条边表示两个点之间有一条路径,而并不是两个点直接相连,这样在寻找增广路的时候就实习了跳跃式连接,因为每个节点没有被访问次数的限制,所以不用考虑这两点的路径中包括了哪些点,能到就完事了。
之后,实现这个预处理使用 floyd传递闭包。传递闭包就是计算任意两个节点之间的连接性,使用动态规划的思想,递推式是**对于任意两个节点 i,j 来说,节点 i 可以到达节点 j 的充要条件是 存在一个节点 k ,节点 i 可以到达节点 k ,节点 k 可以到达节点 j **。


#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define MAXN 505
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return

#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;

int n,m;
bool mp[MAXN][MAXN];
int match[MAXN];
bool used[MAXN];
bool dfs(int v);
void floyd();
int main(){
    ios::sync_with_stdio(false);


    while(cin>>n>>m){
        if(n+m==0)
            break;

        rep(i,0,m){
            int commonA,commonB;
            cin>>commonA>>commonB;
            mp[commonA][commonB]=true;
        }
        floyd();

        int ans=0;
        memset(match,-1,sizeof(match));;
        rep(i,1,n+1){
            memset(used,false,sizeof(used));
            used[i]=true;
            if(dfs(i))
                ans++;

        }

        cout<<n-ans<<endl;

        memset(mp,false,sizeof(mp));
    }

    re 0;
}
bool dfs(int v){
    rep(u,1,n+1){
        if(mp[v][u] && !used[u]){
            used[u]=true;
            int matched=match[u];
            if(matched<0 || dfs(matched)){
                match[u]=v;
                return true;
            }
        }
    }
    return false;
}
void floyd(){
    rep(k,1,n+1)
        rep(i,1,n+1)
            rep(j,1,n+1)
                if(mp[i][k] && mp[k][j])
                    mp[i][j]=true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值