POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包
题目链接:https://vjudge.net/problem/POJ-2594
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 9005 | Accepted: 3680 |
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
Source
题解:
求最小路径覆盖。但与以往不同的是:一个点可以在多条路径上,即一个点可以被走多次,那怎么办呢?
利用Flyod算法求出传递闭包:如果A可以间接走到B,那么我们就直接把AB连起来。
这样,我们就可以按照常规的方法去求最小路径覆盖了。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int n;
int M[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = ; i<=n; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=n; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} void Flyod()
{
for(int k = ; k<=n; k++)
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
M[i][j] = M[i][j]|(M[i][k]&&M[k][j]);
} int main()
{
int m;
while(scanf("%d%d", &n, &m) && (n||m))
{
memset(M, false, sizeof(M));
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
M[u][v] = true;
} Flyod(); //求出传递闭包
int cnt = hungary();
printf("%d\n", n-cnt);
}
}
POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包的更多相关文章
- poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)
http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total ...
- [POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)
传送门 引子: 有一个问题,是对于一个图上的所有点,用不相交的路径把他们覆盖,使得每个点有且仅属于一条路径,且这个路径数量尽量小. 对于这个问题可以把直接有边相连的两点 x —> y,建一个二分 ...
- POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8130 Accepted: 3 ...
- POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 9794 Accepted: 3 ...
- POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
Treasure Exploration Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- POJ-2594 Treasure Exploration,floyd+最小路径覆盖!
Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...
- POJ2594 Treasure Exploration【DAG有向图可相交的最小路径覆盖】
题目链接:http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K T ...
- POJ2594 Treasure Exploration(最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8550 Accepted: 3 ...
- Treasure Exploration---poj2594(传递闭包Floyd+最小路径覆盖)
题目链接:http://poj.org/problem?id=2594 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过(这就是和单 ...
随机推荐
- [SQL]数据库中对值为数字,存储格式为varchar类型的字段进行排序
如果要对数据库中某存储数字的列(存储类型不为int)进行排序,可以在order by 里对该列进行转换, 即如 order by cast(mycolumn as int) desc
- ArrayList练习之存储自定义对象并遍历
新建一个Student.java类 Student.java /* * 这是一个学生类 */ public class Student { private String name; private i ...
- Couchbase III(Python Library)
Couchbase III(Python Library) 第一步 安装 使用pip安装: >pip install couchbase --quiet 确认是否安装成功: >python ...
- 大数据学习——JAVA采集程序
1 需求 从外部购买数据,数据提供方会实时将数据推送到6台FTP服务器上,我方部署6台接口采集机来对接采集数据,并上传到HDFS中 提供商在FTP上生成数据的规则是以小时为单位建立文件夹(2016-0 ...
- Leetcode 289.生命游戏
生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...
- 【优先级队列】Southwestern Europe Regional Contest Canvas Painting
https://vjudge.net/contest/174235#problem/D [题意] 给定n个已知size的帆布,要给这n块帆布涂上不同的颜色,规则是这样的: 每次选择一种颜色C 对于颜色 ...
- Codevs 2693 上学路线(施工)
时间限制: 2 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 问题描述 你所在的城市街道好像一个棋盘,有a条南北方向的街道和b条东西方向的街道. 南 ...
- Python基础教程笔记——第3章:使用字符串
字符串是不可修改的,标准序列操作(索引,分片,判断成员资格,求长度,取最大值 最小值)对字符串都是有效的. 格式化字符串,类似于C语言的输出是的感觉. >>> format=&quo ...
- 动态规划: HDU 1789Doing Homework again
Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...
- 10-JS的函数学习
<html> <head> <title>js的函数学习</title> <meta charset="UTF-8"/> ...