题意

给你张无环有向图,问至少多少条路径能够覆盖该图的所有顶点——并且,这些路径可以有交叉。

思路

不是裸的最小路径覆盖,正常的最小路径覆盖中两个人走的路径不能有重复的点,而本题可以重复。

当然我们仍可将问题转化为最小路径覆盖。如果一个人需要经过另一个人走过的点的时候,让他直接从该点上空飞过去,越过该点,直接走下一个点。如果我们赋予每个人这种能力,那么求得的无重复点的最小路径覆盖结果,就是题目要求的结果,因为需要重复的地方只要飞过去,就可以不重复了。赋予这个能力的方法就是预处理用Floyd求传递闭包把所有点能间接到达的点全都改为直接到达。然后正常求最小路径覆盖即可。

代码

[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, m) for (int i = begin; i < begin+m; i ++)
using namespace std;
const int MAXV = 1005; //|V1|+|V2|
struct MaximumMatchingOfBipartiteGraph{
int vn;
vector <int> adj[MAXV];
void init(int n){ //二分图两点集点的个数
vn = n;
for (int i = 0; i <= vn; i ++) adj[i].clear();
}
void add_uedge(int u, int v){
adj[u].push_back(v);
adj[v].push_back(u);
}
bool vis[MAXV];
int mat[MAXV]; //记录已匹配点的对应点
bool cross_path(int u){
for (int i = 0; i < (int)adj[u].size(); i ++){
int v = adj[u][i];
if (!vis[v]){
vis[v] = true;
if (mat[v] == 0 || cross_path(mat[v])){
mat[v] = u;
mat[u] = v;
return true;
}
}
}
return false;
}
int hungary(){
MEM(mat, 0);
int match_num = 0;
for (int i = 1; i <= vn; i ++){
MEM(vis, 0);
if (!mat[i] && cross_path(i)){
match_num ++;
}
}
return match_num;
}
void print_edge(){
for (int i = 1; i <= vn; i ++){
for (int j = 0; j < (int)adj[i].size(); j ++){
printf("u = %d v = %d\n", i, adj[i][j]);
}
}
}
}match;
bool reach[MAXV>>1][MAXV>>1];
void floyd(int n){
REP(k, 1, n) REP(i, 1, n) REP(j, 1, n){
if (reach[i][j]) continue;
reach[i][j] = reach[i][k] && reach[k][j];
}
}
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n, m;
while(scanf("%d %d", &n, &m), n+m){
if (0 == m){
printf("%d\n", n);
continue;
}
MEM(reach, false);
REP(i, 0, m){
int u, v;
scanf("%d %d", &u, &v);
reach[u][v] = true;
}
floyd(n);
match.init(n);
REP(i, 1, n)
REP(j, 1, n){
if (reach[i][j])
match.add_uedge(i, j+n);
}
printf("%d\n", n-match.hungary());
}
return 0;
}
[/cpp]

POJ 2594 Treasure Exploration (可相交最小路径覆盖)的更多相关文章

  1. POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 9794   Accepted: 3 ...

  2. POJ-2594 Treasure Exploration,floyd+最小路径覆盖!

                                                 Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...

  3. POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8130   Accepted: 3 ...

  4. POJ 2594 Treasure Exploration(最小路径覆盖变形)

    POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...

  5. Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)

    题目链接: Poj 2594 Treasure Exploration 题目描述: 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过 ...

  6. poj 2594 Treasure Exploration (二分匹配)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6558   Accepted: 2 ...

  7. poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)

    http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total ...

  8. POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】

    Treasure Exploration Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  9. POJ 2594 Treasure Exploration 最小可相交路径覆盖

    最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如 ...

随机推荐

  1. Ruby Profiler 详解之 ruby-prof(I)

    项目地址: ruby-prof 在上一篇 Ruby 中的 Profiling 工具中,我们列举了几种最常用的 Profiler,不过只是简单介绍,这一次详细介绍一下 ruby-prof 的使用方法. ...

  2. iOS-JS交互 (WebViewJavascriptBridge)

    , , , );     messageButton.titleLabel.font = font;     messageButton.backgroundColor = [UIColor colo ...

  3. Difference Between Vector and Deque in C++

    1) Dequeue can quickly insert or delete both at the front or the end. However, vector can only quick ...

  4. 自己的gitignore文件

    *.bak*.txt*.vm.gitignore#svn.svn/# built application files*.apk*.ap_ # files for the dex VM*.dex # J ...

  5. unity HideInInspector 默认值 坑 记录 bug

    1:   如果  一个public字段 刚开始有默认值,然后你你觉得这个值不应该给别人看设置为HideInInspector 后,你再在代码里面调整这个默认属性的值,这个时候代码里面调整的值无效.   ...

  6. python_pycharm介绍1

    1. 常用设置 修改编程风格 File-Setting中,Editor下Colors&Fonts修改即可调整风格. 修改字体大小 pycharm默认字体太小,需调整些,Settings--&g ...

  7. MyBatis笔记——Mapper动态代理

    前提概要 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法. Mappe ...

  8. C#与USB HID间的通信

    原文:C#与USB HID间的通信 C#与USBHID接口的通讯相对于与串口间的通讯较为复杂,其中需要多次调用到Windows的一些API.其原理编者尚未全部理清,以下提供简单的USBHID通讯流程. ...

  9. JAVA编译异常处理:java.lang.OutOfMemoryError: PermGen space

    在Intellij开发工具中编译JAVA项目,出现以下错误: 六月 21, 2016 6:28:07 下午 org.apache.tomcat.util.modeler.BaseModelMBean ...

  10. SSIS ->> Logging

    SSIS提供了Event Handler之外的另一种方法捕捉Event和获取需要的信息,这种方法是Logging.SSIS的Logging针对不同的组件可以提供比Event Handler更多的Eve ...