poj2594
特殊的最小路径覆盖
回顾一下经典的最小路径覆盖问题是每个点都恰好被一条路径覆盖
我们把有向无环图的点拆成i,i',对于原图中边i--->j,连边i-->j'
做最大匹配,答案是原图点数-最大匹配
但这道题每个点可以被覆盖多次,所以我们考虑先dfs出每个点可以访问的点
然后拆点,如果i可以到达j,那么连边i-->j',答案是原图点数-最大匹配
为什么呢,感性的想一下,在最小路径覆盖的基础上既然每个点可以被多次经过
那么干脆我们把两个可达的点认为是直接飞过去就好了~ ~
type node=record
point,next:longint;
end; var edge:array[..] of node;
a:array[..,..] of boolean;
p,cx,cy:array[..] of longint;
v:array[..] of boolean;
x,y,n,m,i,j,ans,len:longint; procedure add(x,y:longint);
begin
inc(len);
edge[len].point:=y;
edge[len].next:=p[x];
p[x]:=len;
end; function find(x:longint):longint;
var i,y:longint;
begin
i:=p[x];
while i<>- do
begin
y:=edge[i].point;
if not v[y] then
begin
v[y]:=true;
if (cy[y]=-) or (find(cy[y])=) then
begin
cx[x]:=y;
cy[y]:=x;
exit();
end;
end;
i:=edge[i].next;
end;
exit();
end; procedure dfs(x:longint);
var i,y:longint;
begin
i:=p[x];
v[x]:=true;
while i<>- do
begin
y:=edge[i].point;
if not v[y] then dfs(y);
i:=edge[i].next;
end;
end; begin
readln(n,m);
while (n<>) do
begin
ans:=;
len:=;
fillchar(p,sizeof(p),);
fillchar(a,sizeof(a),false);
for i:= to m do
begin
readln(x,y);
add(x,y);
a[x,y]:=true;
end;
for i:= to n do
begin
fillchar(v,sizeof(v),false);
dfs(i);
for j:= to n do
if v[j] and (i<>j) and not a[i,j] then
add(i,j);
end;
fillchar(cx,sizeof(cx),);
fillchar(cy,sizeof(cy),);
for i:= to n do
if cx[i]=- then
begin
fillchar(v,sizeof(v),false);
ans:=ans+find(i);
end;
writeln(n-ans);
readln(n,m);
end;
end.
poj2594的更多相关文章
- hdu1151+poj2594(最小路径覆盖)
传送门:hdu1151 Air Raid 题意:在一个城镇,有m个路口,和n条路,这些路都是单向的,而且路不会形成环,现在要弄一些伞兵去巡查这个城镇,伞兵只能沿着路的方向走,问最少需要多少伞兵才能把所 ...
- POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包
题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 655 ...
- POJ2594 Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8193 Accepted: 3358 Description Have ...
- poj2594 (最小路径覆盖 + floyd)
题目链接 http://poj.org/problem?id=2594) 题目大意: 一个有向图中, 有若干条连接的路线, 问最少放多少个机器人,可以将整个图上的点都走过. 最小路径覆盖问题. 分析 ...
- poj2594最小顶点覆盖+传递闭包
传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了.. 传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j Have you ever read a ...
- poj2594 机器人寻找宝藏(最小路径覆盖)
题目来源:http://poj.org/problem?id=2594 参考博客:http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641. ...
- POJ2594 Treasure Exploration(最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8550 Accepted: 3 ...
- POJ-2594
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7035 Accepted: 2 ...
- POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 9794 Accepted: 3 ...
随机推荐
- linux device driver —— 字符设备
现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...
- insert当 sql语句里面有变量 为字符类型的时候 要3个单引号
set @InsertStr='INSERT INTO [dbo].[T_SchoolPercentMonth]([SchoolID],[MonthOfYear],[PercentNum]) VALU ...
- JS 模拟C# 字符串格式化操作
/*** ** 功能: 字符串格式化替换操作 ***/ String.prototype.format = function () { var args = arguments; return thi ...
- SDK Manager.exe 无法启动,一闪而过的解决办法
删掉 C:\Windows\system32\ 下的 java.exe.javaw.exe.javaws.exe 即可解决.(转载)
- Html5 部分特性
HTML5 是 W3C 与 WHATWG 合作的结果. 编者注:W3C 指 World Wide Web Consortium,万维网联盟. 编者注:WHATWG 指 Web Hypertext Ap ...
- .Net 4.0 Convert Object to XDocument
将Object转换为XDocment对象 代码如下: C# – Object to XDocument using System; using System.Collections.Generic; ...
- sqlserver触发器如何将一个库中的数据插入到另外一个库中
需求:实现的功能就是,查询当前表的所有信息,插入到另外一个库中(同一台机器,同一个SqlServer) 解决:insert into dB2.dbo.TB2 select * from db1.dbo ...
- 制作SSL证书
上一节介绍了OpenSSL的目录结构,本节介绍一下SSL证书的制作. OpenSSL安装后建议把其中的bin目录添加到系统环境变量中,方便以后操作. 建立一个新的目录SSL专门用来制作证书. 建立证书 ...
- ubuntu系统安装的MySql数据库,远程不能访问的几种可能问题
安装MySQL数据库后一般会遇到远程计算机不能连接的问题,具体问题需要我们排查.可能一:MySql数据库是否提供了外部访问的用户以及权限?可能二:MySql的配置文件是否只绑定了本机ip(ubuntu ...
- idea配置tomcat.md
[toc] 1.打开Edit Configurations,可以通过万能搜索快速进入!!! 2.添加服务器,在左上角找到Tomcat并添加 3.配置发布路径,Server标签页中填写完名称和路径,在D ...