POJ - 2594 Treasure Exploration(最小路径覆盖+floyd)

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个点,一些点之间有单向边连接,现在有一种机器人能顺着这些边来访问点,两个不同机器人的道路可能包含一些相同的点。要求最少需要多少机器人,才能访问完所有的点。

思路:看到这题发现和我之前做的一道最小路径覆盖的题目很像,唯一不同的就是那题的点是不能重复的,但是这题的点是可以重复经过的,一组样例可以来解释区别
在这里插入图片描述
如果能重复经过的话就需要2个机器人,如果不能重复经过就需要3个。然后回到这里,怎么求可以重复经过的,比如有两条边一个是1-2,另一个是2-3,这样的话其实就可以重新建立一条1-3的边,因为他们之间是可以相互到达的,所以可以用flody跑一遍,把能到达的点相互之间都连边。最后求最大匹配数,需要最少机器人的个数就等于点的总数-最大匹配数。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;

#define maxn 5010
#define ll long long
#define inf 0x3f3f3f3f
#define mod 998244353


int vis[maxn],match[maxn];
int mp[550][550];
int n,m;


void floyd()
{
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(mp[i][k]==1&&mp[k][j]==1)
                    mp[i][j]=1;
}

int dfs(int u)
{
    for(int v=1; v<=n; v++)
    {
        if(!vis[v]&&mp[u][v])
        {
            vis[v]=1;
            if(!match[v]||dfs(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        if(n==0&&m==0) break;
        memset(match,0,sizeof(match));
        memset(mp,0,sizeof(mp));
        int x,y;
        for(int i=0; i<m; i++)
        {
            scanf("%d %d",&x,&y);
            mp[x][y]=1;
        }
        floyd();
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        printf("%d\n",n-ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值