【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)

题干:

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个点的有向图,问最少放几个机器人能走完整个图,机器人可以任意选一个点放置,一个点可以被多个机器人经过(例如1->3,2->3,3->4,3->5.这种情况需要两个机器人),保证图中没有环, 不保证图连通。

解题报告:

因为我们要选择最少的路径来覆盖所有的点,那么显然这是一个最小路径覆盖问题。但是这题有个问题就在于一个点可以被多个机器人经过(这就是和单纯的最小路径覆盖的区别)。如果我们用裸的最小路径覆盖,那么“题目大意”里给的那个样例应该输出3,但是其实应该输出2,这也就是说我们应该让每一个点可以到达的点全都置为true,然后再跑二分图,这样得到的才是正解、

做法就是求个floyd传递闭包,其实邻接表建图然后用Dijkstra也可以,好像这样更快?反正floyd是800ms才水过。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 555 + 5;
bool f[MAX][MAX];
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(f[i][k] && f[k][j]) f[i][j] = 1;}}}	
}
bool used[MAX];
int nxt[MAX];
bool find(int x) {for(int i = 1; i<=n; i++) {if(used[i] == 0 && f[x][i]) {used[i] = 1;if(nxt[i] == 0|| find(nxt[i])) {nxt[i] = x;return 1;} }}return 0 ;
}
int match() {int res = 0;for(int i = 1; i<=n; i++) {memset(used,0,sizeof used);if(find(i)) res++;}return res;
}
int main()
{while(~scanf("%d%d",&n,&m) && n+m) {memset(f,0,sizeof f);memset(nxt,0,sizeof nxt);for(int a,b,i = 1; i<=m; i++) {scanf("%d%d",&a,&b);f[a][b]=1;}floyd();printf("%d\n",n - match());}return 0 ;
}

 

总结:

这题可以用强连通分量吗?

这题说了没有环,也就是没有强连通分量啊,tarjan没用啊。。。

能不能用树形dp?

说了没有环不代表他就是树,比如这个样例:

8 8
1 3
2 3 
3 4 
3 5 
4 6 
5 6 
6 7 
6 8 
最终应该输出2

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440340.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

6.Random Forests

Introduction 决策树会让您做出艰难的决定。 有很多树叶的深树将会过拟合&#xff0c;因为每个预测都来自其叶子上只有少数房屋的历史数据。 但是叶子很少的浅树会表现不佳&#xff0c;因为它无法捕获原始数据中的许多区别。 即使在今天&#xff0c;最成熟的建模技术也面临着过…

7.Handling Missing Values

本教程是学习机器学习课程的第2部分。 本教程选择了1级完成的位置&#xff0c;因此如果您从1级完成练习&#xff0c;您将获得最大的收益。 在此步骤中&#xff0c;您将学习三种处理缺失值的方法。 然后&#xff0c;您将学习如何比较这些方法在任何给定数据集上的有效性。 Intr…

打开电脑计算机超级慢,手把手教你电脑开机慢怎么办

等到花都谢了&#xff0c;你怎么还不开机&#xff1f;这电脑开机真是离奇的慢&#xff0c;有心将它换了&#xff0c;奈何兜里空空。凑合着用又无法忍受这种煎熬。其实你只需要用鼠标点几下就可以不用等待这漫长的开机过程了。高铁&#xff0c;飞机&#xff0c;网络&#xff0c;…

【POJ - 1486】Sorting Slides(思维建图,二分图求必须边,关建边,图论)

题干&#xff1a; Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not a very tidy person and has put all his transparencies on one big heap. Before giving the talk, he has to sort the slides. Being a kind of minima…

用OpenSSL编写SSL,TLS程序

http://zhoulifa.bokee.com/6134045.html http://blog.sina.com.cn/s/blog_86ca13bb0100vaph.html http://blog.chinaunix.net/uid-26575352-id-3048856.html 一、简介: SSL(SecureSocket Layer)是netscape公司提出的主要用于web的安全通信标准,分为2.0版和3.0版.TLS(Transport…

信息技术计算机伦理与安全教案,龙教版信息技术七年级下册第7课 安全与道德 教案...

ID:9954219分类&#xff1a;全国,2019资源大小&#xff1a;228KB资料简介:题 目第七课 安全与道德总课时1设计来源自我设计教学时间教材分析这节课计算机与网络安全部分定义介绍和叙述较多,所以为了避免枯燥可以设计课件和并准备病毒计算机安全报道的视频、多媒体讲解、图片等…

【HDU - 5706】GirlCat(bfs)

题干&#xff1a; As a cute girl, Kotori likes playing Hide and Seek with cats particularly. Under the influence of Kotori, many girls and cats are playing Hide and Seek together. Koroti shots a photo. The size of this photo is nmnm, each pixel of the ph…

8.Using Categorical Data with One Hot Encoding

本教程是机器学习系列的一部分。 在此步骤中&#xff0c;您将了解“分类”变量是什么&#xff0c;以及处理此类数据的最常用方法。 Introduction 分类数据是仅采用有限数量值的数据。 例如&#xff0c;如果人们回答一项关于他们拥有哪种品牌汽车的调查&#xff0c;结果将是明…

iPhone换屏幕测试软件,怎样检验iPhone是否更换过屏幕?

原标题&#xff1a;怎样检验iPhone是否更换过屏幕&#xff1f;关注下图公众号&#xff0c;鉴定苹果手机真假↓↓↓购买新手机时&#xff0c;到手后会想手机各零部件是否是正品原装&#xff0c;就好比屏幕是否原装屏&#xff01;入手一部iPhone新机的时候&#xff0c;该如何检验…

*【HDU - 5707】Combine String(dp)

题干&#xff1a; Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of aa and bb. A string cc is said to be the combine string of aa and bb if and only if cc can be broken into two subsequences, when you read the…

《TCP/IP详解》学习笔记(一):基本概念

为什么会有TCP/IP协议 在世界上各地&#xff0c;各种各样的电脑运行着各自不同的操作系统为大家服务&#xff0c;这些电脑在表达同一种信息的时候所使用的方法是千差万别。就好像圣经中上帝打乱 了各地人的口音&#xff0c;让他们无法合作一样。计算机使用者意识到&#xff0c;…

【POJ - 3272】Cow Traffic(dp,建反向图,DAG拓扑图)

题干&#xff1a; The bovine population boom down on the farm has caused serious congestion on the cow trails leading to the barn. Farmer John has decided to conduct a study to find the bottlenecks in order to relieve the traffic jams at milking time. The…

pc服务器不同型号,服务器与PC系统软件之不同

服务器与PC系统软件之不同对于中关村在线的网友来说&#xff0c;PC系统应该都不陌生&#xff0c;而且分分钟重装的水准。但在笔者过往的服务器装机经验中&#xff0c;可谓是一部千年血泪史。服务器和PC系统差别还是很大的。现在的PC系统多是windows7和windows10&#xff0c;而在…

9.XGBoost

本教程是机器学习系列的一部分。 在此步骤中&#xff0c;您将学习如何使用功能强大的xgboost库构建和优化模型。 What is XGBoost XGBoost是处理标准表格数据的领先模型&#xff08;您在Pandas DataFrames中存储的数据类型&#xff0c;而不是像图像和视频这样的更奇特的数据类…

*【HDU - 5711】Ingress(tsp旅行商问题,优先队列贪心,状压dp,floyd最短路,图论)

题干&#xff1a; Brickgao, who profited from your accurate calculating last year, made a great deal of money by moving bricks. Now he became gay shy fool again and recently he bought an iphone and was deeply addicted into a cellphone game called Ingress. …

ajax get请求成功,成功()函数的AJAX GET请求

后不叫我有一个jQuery的AJAX脚本像下面&#xff1a;成功()函数的AJAX GET请求function FillCity() {var stateID $("#ddlState").val();$.ajax({url: Url.Action("Employee", "Index"),type: "GET",dataType: "json",data:…

《TCP/IP详解》学习笔记(二):数据链路层

数据链路层有三个目的&#xff1a; 为IP模块发送和 接收IP数据报。为ARP模块发送ARP请求和接收ARP应答。为RARP发送RARP请 求和接收RARP应答ip大家都听说过。至于ARP和RARP&#xff0c;ARP叫做地址解析协议&#xff0c;是用IP地址换MAC地址的一种协议&#xff0c;而RARP则叫…

【POJ - 2762】Going from u to v or from v to u?(Tarjan缩点,树形dp 或 拓扑排序,欧拉图相关)

题干&#xff1a; In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the o…

《TCP/IP详解》学习笔记(三):IP协议、ARP协议

把这三个协议放到一起学习是因为这三个协议处于同一层,ARP 协议用来找到目标主机的 Ethernet 网卡 Mac 地址,IP 则承载要发 送的消息。数据链路层可以从 ARP 得到数据的传送信息,而从 IP 得到要传输的数据信息。 IP 协议 IP 协议是 TCP/IP 协议的核心,所有的 TCP,UDP,IMCP,IGCP…

光与夜之恋服务器维护中,光与夜之恋7月16日停服维护说明 维护详情一览

光与夜之恋7月16日停服维护说明维护详情一览。光与夜之恋7月16日停服维护更新了哪些内容?我们去了解一下。【7月16日停服维护说明】亲爱的设计师&#xff1a;为了给设计师们提供更好的游戏体验&#xff0c;光启市将于7月16日(周五)00:00进行预计5小时的停服维护&#xff0c;可…