[POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)
引子:
有一个问题,是对于一个图上的所有点,用不相交的路径把他们覆盖,使得每个点有且仅属于一条路径,且这个路径数量尽量小。
对于这个问题可以把直接有边相连的两点 x —> y,建一个二分图 x' —> y,最后 节点数 - 最大匹配数 即为最终答案。
这才是题解:
但是这个题目不同,此题问的是用一些路径把所有点覆盖,并没有说每个点有且仅属于一条路径,所以需要求这个图的传递闭包。
把可达的两点建立二分图。最后 节点数 - 最大匹配数 即为最终答案。
可以看看这篇blog
——代码
#include <cstdio>
#include <cstring>
#define M(x, a) memset(a, x, sizeof(a)); using namespace std; const int MAXN = ;
int n, m, ans, cnt;
int belong[MAXN];
bool a[MAXN][MAXN], vis[MAXN]; inline bool find(int i)
{
int j;
for(j = ; j <= n; j++)
if(!vis[j] && a[i][j])
{
vis[j] = ;
if(!belong[j] || find(belong[j]))
{
belong[j] = i;
return ;
}
}
return ;
} int main()
{
int i, j, k, x, y;
while(scanf("%d %d", &n, &m) && n + m)
{
ans = ;
M(, a);
M(, belong);
for(i = ; i <= m; i++)
{
scanf("%d %d", &x, &y);
a[x][y] = ;
}
for(k = ; k <= n; k++)
for(i = ; i <= n; i++)
for(j = ; j <= n; j++)
a[i][j] = a[i][j] || (a[i][k] && a[k][j]);
for(i = ; i <= n; i++)
{
memset(vis, , sizeof(vis));
if(find(i)) ans++;
}
printf("%d\n", n - ans);
}
}
[POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)的更多相关文章
- POJ2594 Treasure Exploratio —— 最小路径覆盖 + 传递闭包
题目链接:https://vjudge.net/problem/POJ-2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 655 ...
- poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)
http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total ...
- cogs 728. [网络流24题] 最小路径覆盖问题 匈牙利算法
728. [网络流24题] 最小路径覆盖问题 ★★★☆ 输入文件:path3.in 输出文件:path3.out 评测插件时间限制:1 s 内存限制:128 MB 算法实现题8-3 最 ...
- POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8130 Accepted: 3 ...
- POJ-2594 Treasure Exploration,floyd+最小路径覆盖!
Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...
- 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 ...
- 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 ...
随机推荐
- [BZOJ2761][JLOI2011]不重复数字 暴力
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2761 直接暴力. #include<cstdio> #include<c ...
- Android小玩意儿-- 从头开发一个正经的MusicPlayer(一)
之前从未接触过音乐播放器这块东西的开发.今天偶然想做一个自己的音乐播放器.算是练练手.既然要做,就要做一个正儿八经的App.很多网上的资料也是模模糊糊,不是很全,现在开始,自己摸索着尝试着一步一步的做 ...
- Verilog设计中的锁存器
问题: 什么是锁存器? 什么时候出现锁存器? 锁存器对电路有什么影响? 如何在FPGA设计中避免锁存器? 在FPGA设计中应该避免锁存器.实际上,锁存器与D触发器实现的逻辑功能基本相同,都有暂存数据的 ...
- 详解nginx.conf文件配置项(包括负载均衡)
http://www.cnblogs.com/hsapphire/archive/2010/04/08/1707109.html #运行用户 user nobody nobody; #启动进程 wo ...
- uva1628 Pizza Delivery
fixing great wall 的变形dp(i,j,k,p)不考虑i-j的客人,还要送k个人,目前位置在p起点i和总数量k都要枚举dp(i,j,k,p)=max(dp(m,j,k-1,p)+val ...
- 前端基础入门第一阶段-Web前端开发基础环境配置
Web前端和全栈的定义: A.什么是传统传统web前端:需要把设计师的设计稿,切完图,写标签和样式,实现JS的效果,简而言之即只需要掌握HTML的页面结构,CSS的页面样式,javaScript页面的 ...
- error C2143: 语法错误 : 缺少“;”(在“&”的前面)
报错: error C2143: 语法错误 : 缺少“;”(在“&”的前面) 代码: #include <iostream> ostream & << (ost ...
- zabbix auto registration
1./etc/zabbix/zabbix_agent.conf serverActive=zabbix server ip 2.frontend configuration>actions> ...
- js hover 下拉框
<div class="box"> <div class="a f">111111</div> <div class= ...
- 定位 absolute和relative比较
absolute:脱离原来位置定位.是相对于最近的有定位的父级进行定位;如果没有有定位的父级元素,就相对文档进行定位 relative:保留原来位置进行定位,相对于自己原来的位置进行定位 下面举两个例 ...