[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 ...
随机推荐
- Vue nextTick 理解
官网解释: 将回调延迟到下次 DOM 更新循环之后执行.在修改数据之后立即使用它,然后等待 DOM 更新.它跟全局方法 Vue.nextTick 一样,不同的是回调的 this 自动绑定到调用它的实例 ...
- JUnit的好搭档-Hamcrest
一.Hamcrest简介 Hamcrest是一个用于编写匹配器(matcher)对象的框架,允许以声明方式定义“匹配(match)”规则.它可以与JUnit框架配合使用,使断言可读更高,更加灵活(例如 ...
- mysql5.5版本导入sql报错
1 在5.5的版本中不支持utf8_german2的类型 2 在5.5版本中只支持timestamp类型设置默认值为CURRENT_TIMESTAMP,不支持datetime类型默认值设置为CURRE ...
- Java单例模式的6种写法
在Java中,单例有很多种写法,面试时,手写代码环节,除了写算法题,有时候也会让手写单例模式,这里记录一下单例的几种写法和优缺点. 初级写法 懒汉式 饿汉式 双锁检验 内部类 枚举式 1.初级写法 p ...
- 【数据分析 R语言实战】学习笔记 第五章 数据的描述性分析(下)
5.6 多组数据分析及R实现 5.6.1 多组数据的统计分析 > group=read.csv("C:/Program Files/RStudio/002582.csv") ...
- 用JS获取Html中所有图片文件流然后替换原有链接
function displayHtmlWithImageStream(bodyHtml) { var imgReg = /<img.*?(?:>|\/>)/gi; var arr ...
- css3纯手写loading效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [Windows Server 2012] 安装IIS8.5及FTP
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:安装IIS ...
- 如何用sql server数据库恢复.bak数据库备份
@hcy(黄灿奕) 之前有两次都恢复不了,折腾了很长时间,这一次碰到这样的问题,居然又忘了,又捣鼓了很长时间,现在记下来 1.右击SQL Server 2008实例下的“数据库”文件夹.就是与安全性. ...
- Tomcat7的安装与配置
Tomcat的安装及配置我曾经使用过tomcat,并实现了一个简单的servlet程序.没想到再次安装tomcat的时候用浪费了我大半天的时间.我想有必要做个总结,否则下次不知又要花费我多少时间. 1 ...