Treasure Exploration POJ - 2594 【有向图路径可相交的最小路径覆盖】模板题
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
Output
Sample Input
1 0
2 1
1 2
2 0
0 0
Sample Output
1
1
2
题意:在外星上有n个点需要机器人去探险,有m条单向路径。问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过(这就是和单纯的最小路径覆盖的区别)。
思路:这是个最小路径覆盖问题,但是因为有的点可以重复访问,所以最小路径是可以相交的,我们就用传递闭包建立新图(G’),转化为一般的路径覆盖,然后就是跟 poj1422 一样了。
最小路径覆盖 = 图的顶点数 – 最大匹配数,所以只要用匈牙利算法求出最大匹配数,然后用顶点数一减就出来了。
AC代码:
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 666
int match[maxn];
int vis[maxn];
int e[maxn][maxn];
int n,m;
int dfs(int u){
for(int i=;i<=n;i++){
if(!vis[i]&&e[u][i]){
vis[i]=;
if(match[i]==||dfs(match[i])){
match[i]=u;
return ;
}
}
}
return ;
}
void floyd(){
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(e[i][k]&&e[k][j])
e[i][j]=;
}
int main(){
while(~scanf("%d%d",&n,&m)&&(n+m)){
int x,y;
memset(match,,sizeof(match)); for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
e[x][y]=;
}
floyd();
int ans=;
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
if(dfs(i))
ans++;
}
int res=n-ans;
printf("%d\n",res);
for(int i=;i<=maxn;i++)
for(int j=;j<=maxn;j++)
e[i][j]=;
}
return ;
}
Treasure Exploration POJ - 2594 【有向图路径可相交的最小路径覆盖】模板题的更多相关文章
- K - Treasure Exploration - POJ 2594(最小路径覆盖+闭包传递)
题意:给一个有向无环图,求出来最小路径覆盖,注意一个点可能会被多条路径重复 分析:因为有可能多条路径走一个点,可又能会造成匹配的不完全,所以先进行一次闭包传递(floyd),然后再用二分匹配的方法求出 ...
- poj 2594Treasure Exploration(有向图路径可相交的最小路径覆盖)
1 #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> ...
- Treasure Exploration POJ - 2594(最小边覆盖)
因为是路 所以 如果 1——3 2——3 3——4 3——5 则 1——4 1——5 2——4 2——5 都是是合法的 又因为机器人是可以相遇的 所以 我们把所有的点 分别放在 ...
- loj 1429(可相交的最小路径覆盖)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1429 思路:这道题还是比较麻烦的,对于求有向图的可相交的最小路径覆盖,首先要解决成环问 ...
- POJ2594 Treasure Exploration【DAG有向图可相交的最小路径覆盖】
题目链接:http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K T ...
- poj 2594(可相交的最小路径覆盖)
题目链接:http://poj.org/problem?id=2594 思路:本来求最小路径覆盖是不能相交的,那么对于那些本来就可达的点怎么处理,我们可以求一次传递闭包,相当于是加边,这样我们就可以来 ...
- POJ 1422 Air Raid(二分图匹配最小路径覆盖)
POJ 1422 Air Raid 题目链接 题意:给定一个有向图,在这个图上的某些点上放伞兵,能够使伞兵能够走到图上全部的点.且每一个点仅仅被一个伞兵走一次.问至少放多少伞兵 思路:二分图的最小路径 ...
- POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ:3020-Antenna Placement(二分图的最小路径覆盖)
原题传送:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Descri ...
随机推荐
- PPT 中用 LaTeX 插入公式、PowerPoint 中用 LaTeX 插入公式(4)
步骤: 1. 安装 CTex 2. 安装 IguanaTex >> 下载链接1:官网 >> 下载链接2:复制链接到迅雷或IDM下载很快 3. 将「IguanaTex_v1_56 ...
- Django打印出在数据库中执行的语句
有时我们需要看models操作时对应的SQL语句, 可以用如下方法查看--- 在django project中的settings文件尾部添加如下代码 LOGGING = { 'version': 1, ...
- yii2 migrate 数据库迁移的简单分享
开发中经常会用到的方法小结: 1../yii migrate xxx_xx 在表中插入某字段 : public function up() {$this->addColumn('{{applic ...
- 其实每个行业都有各自的辛苦,好的程序员并不累,他们乐此不疲(见过太多在职位事业、人生方向上随转如流的人,累了疲乏了就去做别的事情了。必须有自己的坚守和立足的点,自我驱动,否则沦为在别人的体制制度中被驱赶一生)good
作者:陈柯好链接:https://www.zhihu.com/question/39813913/answer/104275537来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Unity场景间数据传递方法
在游戏开发中,会常用到场景间传递数据的方法(比如关卡选择,过关后自动回到关卡选择界面,以动画方式解锁下一关),目前研究了三种: 1. 使用DontDestroyOnLoad方法: (1)在场景A中做个 ...
- 设置pictureBox的边框颜色(转载)
原文地址:https://www.cnblogs.com/hardsoftware/p/5720545.html private void pictureBox2_Paint(object sende ...
- my SO 链接opencv静态库一些FUCKing的笔记 opencv410 有毒
1. 2. CMake "/work/lib/opencv/ubuntu14/4.1.0" make[2]: *** No rule to make target `/usr/lo ...
- uni-app入门学习
什么是 uni-app 1 uni-app 是一个使用 Vue.js 开发跨平台应用的前端框架,开发者编写一套代码,可编译到iOS.Android.H5.小程序等多个平台. 官方的体验例子: 2 un ...
- javascript经常用到的函数
trim函数: trim() lTrim() rTrim()校验字符串是否为空: checkIsNotEmpty(str ...
- # 机器学习算法总结-第九天(XGboost)