POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 600;
const int INF = 0x3f3f3f3f;
vector<int>G[maxn];
int Mx[maxn], My[maxn], dx[maxn], dy[maxn], used[maxn], dis;
int Map[maxn][maxn];
bool SearchP(int _n){
queue<int>Q;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
int dis = INF;
for(int i = 1; i <= _n; i++){
if(Mx[i] == -1){
dx[i] = 0;
Q.push(i);
}
}
int v;
while(!Q.empty()){
int u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(dy[v] == -1){
dy[v] = dx[u] + 1;
if(My[v] == -1){
dis = dy[v];
}else{
dx[My[v]] = dy[v] + 1;
Q.push(My[v]);
}
}
}
}
return dis != INF;
}
int dfs(int u){
int v;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i];
if(!used[v] && dy[v] == dx[u] + 1){
used[v] = 1;
if(My[v] != -1 && dy[v] == dis){
continue;
}
if(My[v] == -1 || dfs(My[v])){
Mx[u] = v;
My[v] = u;
return true;
}
}
}
return false;
}
int MaxMatch(int ln,int rn){
int ret = 0;
memset(Mx,-1,sizeof(Mx));
memset(My,-1,sizeof(My));
while(SearchP(ln)){
memset(used,0,sizeof(used));
for(int i = 1; i <= ln; i++){
if(Mx[i] == -1 && dfs(i)){
ret++;
}
}
}
return ret;
}
int main(){
int T, cas = 0, n, m, N, M, k;
while(scanf("%d%d",&N,&M)!=EOF&&(N+M)){
for(int i = 0; i <= N; i++){
G[i].clear();
}
int a,b;
memset(Map,0,sizeof(Map));
for(int i = 1; i <= M; i++){
scanf("%d%d",&a,&b);
Map[a][b] = 1;
}
for(int k = 1; k <= N; k++){
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
Map[i][j] = Map[i][j]|| Map[i][k]&&Map[k][j];
}
}
} for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(Map[i][j]){
G[i].push_back(j);
}
}
}
n = m = N;
int res = MaxMatch(n,m);
printf("%d\n",N-res);
}
return 0;
}
POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】的更多相关文章
- poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)
http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total ...
- Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)
题目链接: Poj 2594 Treasure Exploration 题目描述: 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过 ...
- POJ 2594 Treasure Exploration(最小路径覆盖变形)
POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...
- POJ 2594 Treasure Exploration (可相交最小路径覆盖)
题意 给你张无环有向图,问至少多少条路径能够覆盖该图的所有顶点--并且,这些路径可以有交叉. 思路 不是裸的最小路径覆盖,正常的最小路径覆盖中两个人走的路径不能有重复的点,而本题可以重复. 当然我们仍 ...
- POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)
题意: 派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.有的点可以重复去. 输入数据: 首先是n和 ...
- poj 2594 Treasure Exploration(最小路径覆盖,可重点)
题意:选出最小路径覆盖图中所有点,路径可以交叉,也就是允许路径有重复的点. 分析:这个题的难点在于如何解决有重复点的问题-方法就是使用Floyd求闭包,就是把间接相连的点直接连上边,然后就是求最小路径 ...
- POJ 2594 Treasure Exploration (Floyd+最小路径覆盖)
<题目链接> 题目大意: 机器人探索宝藏,有N个点,M条边.问你要几个机器人才能遍历所有的点. 解题分析: 刚开始还以为是最小路径覆盖的模板题,但是后面才知道,本题允许一个点经过多次,这与 ...
- POJ 2594 Treasure Exploration 最小可相交路径覆盖
最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如 ...
- poj 2594 Treasure Exploration (二分匹配)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 6558 Accepted: 2 ...
随机推荐
- 怎么用Shell连接VirtualBox Linux虚拟机,在Mac电脑上
问题描述 由于VirtualBox采用桥接的方式连接网络,所以不能在Mac上直接访问虚拟机. 解决思路和办法 由于不能直连,但VirtualBox支持端口转发功能,可以设定转发规则,绑定宿主机和虚拟机 ...
- spring分布式事务学习笔记(2)
此文已由作者夏昀授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Model类如下:package com.xy.model 1 package com.xy.model; ...
- tomcat - 认识
tomcat - web应用服务器 环境:ubuntu测试 @shell命令(cd到tomcat目录下) 启动: ./bin startup.sh 关闭:./bin shutdown.sh @部署 ...
- Crontab 驱动 Scrapy 定时任务
做了个爬虫去定时抓自己发布在cnblogs更新的文章,考虑用corntab定时任务驱动 crontab 任务配置 crontab配置规则就不啰嗦了,网上很多介绍,规则很容易功能却很强大. 然后我发现只 ...
- DP【洛谷P4290】 [HAOI2008]玩具取名
P4290 [HAOI2008]玩具取名 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后他会根据自己的喜好,将名字中任意一个字母用"WI ...
- 微信小程序生成带参二维码
需求:生成小程序中的海报,需要小程序二维码可以使用户保存到本地在朋友圈分享 生成二维码工具类代码如下: package com.aone.foottalk.action.wx.util; import ...
- VisualSVN的安装使用
1.什么是VisualSVN VisualSVN Server是集成了Subversion和Apache的一种版本管理工具,它简化了手工配置Subversion的繁琐步骤,安装的时候SVN Serve ...
- ftp 添加用户及修改用户目录
添加用户 : useradd 用户名 -s /sbin/nologin //限定用户test不能telnet,只能ftp; usermod -s /sbin/bash 用户名 //用户恢复正常 ;该账 ...
- javascript中的一元操作符
题目如下: var s1 = "01"; var s2 = "1.1"; var s3 = "z"; var b = false; var ...
- python3 提取http请求response中的某个值
在使用python3 request做接口测试的时候,想获取response的json中的某个值做断言时,发现request好像没有相关的方法 所以只好自己找写一个了.在我看来,json就是一个字典, ...