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的openfire运行日志配置经历
openfire的日志可以通过/usr/openfire/lib/log4j.xml(与openfire的安装目录有关,我的openfire是安装在/usr/openfire/)的xml配置文件进行设 ...
- (转)if语句优化
一.使用常见的三元操作符 if (foo) bar(); else baz(); ==> foo?bar():baz(); if (!foo) bar(); else baz(); ==> ...
- 在Java控制台模拟dos命令下操作MySQL
一.导入mysql的驱动:"com.mysql.jdbc.Driver", 必须将mysql的驱动 包(jar文件)导入到本工程中. Class.forName("com ...
- app包中的fragment和v4包中的fragment的使用的区别
app包中的fragment和v4包中的fragment的使用的区别 1.尽量不要用app包中的fragment,因为这个是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的 2.androi ...
- padding and margin.
padding is the space between the content and the border, whereas margin is the space outside the bor ...
- 【SSMS增强工具】SQL Sharper 2014介绍
产品介绍 SQL Sharper是一款SQL Server Management Studio插件,用于数据库对象快速查询.表结构查询.优化查询结果导出.代码生成等方面. 适用人群:T-SQL开发者. ...
- oracle中用comment on的用法
oracle中用comment on命令给表或字段加以说明,语法如下:COMMENT ON { TABLE [ schema. ] { table | view } | COLUMN [ s ...
- java中怎么进行字符串替换?
String str = "test.doc"; String newStr = str.replaceAll("doc","html");
- What and where are the stack and heap?
The stack is the memory set aside as scratch space for a thread of execution. When a function is cal ...
- 2016.7.16equals的使用(一)
class V{ private int a; V(int a){ rhis a=a; } public boolean equals(int a,int b){ if(this.a equals( ...