题意

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

思路

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

当然我们仍可将问题转化为最小路径覆盖。如果一个人需要经过另一个人走过的点的时候,让他直接从该点上空飞过去,越过该点,直接走下一个点。如果我们赋予每个人这种能力,那么求得的无重复点的最小路径覆盖结果,就是题目要求的结果,因为需要重复的地方只要飞过去,就可以不重复了。赋予这个能力的方法就是预处理用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. HDU1542 Atlantis(矩形面积并)

    #pragma warning (disable:4996) #include<iostream> #include<cstring> #include<string&g ...

  2. iOS富文本-NSAttributedString简单封装

    直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) {         ; i < styles.count; i ++) {      ...

  3. WCF 传输和接受大数据

    向wcf传入大数据暂时还没找到什么好方案,大概测了一下传输2M还是可以的,有待以后解决. 接受wcf传回的大数据,要进行web.config的配置,刚开是从网上搜自己写进行配置,折磨了好长时间. 用以 ...

  4. Visual Studio 项目中添加include, lib, dll库文件(*.h,*.lib,*.dll)

    应用程序使用外部库时需要进行加载,两种库的加载本质上都是一样:提供功能和功能的定义.vs2005 c++ 项目设置外部库方法如下: 1. 添加编译所需要(依赖)的 lib 文件     在“项目-&g ...

  5. 初学Ajax(三)

    $.ajax() $.ajax()是所有ajax方法中最底层的方法,所有其他方法都是基于$.ajax()方法的封装.这个方法只有一个参数,传递一个各个功能键值对的对象. $.ajax()方法对象参数表 ...

  6. PHP Simple HTML DOM解析器

    一直以来使用php解析html文档树都是一个难题.Simple HTML DOM parser 帮我们很好地解决了使用 php html 解析 问题.可以通过这个php类来解析html文档,对其中的h ...

  7. 李洪强经典iOS面试题11

        #import 跟#include 又什么区别,@class呢, #import<> 跟 #import””又什么区别? #import是Objective-C导入头文件的关键字, ...

  8. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-003Table per concrete class with unions(@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)、<union-subclass>)

    一.代码 1. package org.jpwh.model.inheritance.tableperclass; import org.jpwh.model.Constants; import ja ...

  9. WordPress主题制作教程4:调用指定页面内容

    假设页面page_id=86 $page_id = 86; echo "标题:".get_post($page_id)->post_title; echo "内容: ...

  10. dojo 十一 jsonp

    官方教程:Getting Jiggy with JSONPDojo对Ajax实现的框架XHR的功能很强大,但 XHR 框架的函数有一问题就是不能跨域访问,浏览器不允许 XHR 对象访问其他域的站点.此 ...