DAG图上可相交最小路径覆盖

先求给定DAG的传递闭包,将任意相连的两点加入二分图中,然后就是经典的不相交最小路径覆盖

所谓传递闭包就是将DAG图中任意点间的连通关系处理出来,用Floyd即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return fh*rv;
}
int n,m,dis[505][505],g[505][505],match[505];
bool f[505];
bool hungarian(int u){
for(int i=1;i<=g[u][0];i++){
int v=g[u][i];
if(!f[v]){
f[v]=1;
if(!match[v]||hungarian(match[v])){
match[v]=u;
return 1;
}}
}
return 0;
}
int main(){
while(1){
n=init();m=init();
if(!n&&!m) break;
memset(dis,0,sizeof(dis));
memset(g,0,sizeof(g));
memset(match,0,sizeof(match));
for(int i=1;i<=m;i++){
int u=init(),v=init();
dis[u][v]=1;
}
for(int k=1;k<=n;k++){
for(int j=1;j<=n;j++){
for(int i=1;i<=n;i++){
dis[i][j]=dis[i][j]||(dis[i][k]&&dis[k][j]);
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(dis[i][j]) g[i][++g[i][0]]=j;
}
}
int ans=0;
for(int i=1;i<=n;i++){
memset(f,0,sizeof(f));
if(hungarian(i)) ans++;
}
printf("%d\n",n-ans);
}
}

POJ [P2594] Treasure Exploration的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. poj 2594 Treasure Exploration 二分图匹配

    点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7215   ...

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

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

  8. POJ 2594 Treasure Exploration (可相交最小路径覆盖)

    题意 给你张无环有向图,问至少多少条路径能够覆盖该图的所有顶点--并且,这些路径可以有交叉. 思路 不是裸的最小路径覆盖,正常的最小路径覆盖中两个人走的路径不能有重复的点,而本题可以重复. 当然我们仍 ...

  9. POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)

    题意:  派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.有的点可以重复去. 输入数据: 首先是n和 ...

随机推荐

  1. hdu_3483A Very Simple Problem(C(m,n)+快速幂矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3483 A Very Simple Problem Time Limit: 4000/2000 MS ( ...

  2. kali中的webshell

    webacoo -g 生成一句话 -o 输出文件 -r 不混淆代码 -t 连接模式 -u 制定URL 生成一句话 webacoo -g -o a.php 连接一句话 webacoo -t -u htt ...

  3. flume1.8 Interceptors拦截器(五)

    1. Flume Interceptors Flume有能力修改/删除流程中的events.这是在拦截器(interceptor)的帮助下完成的.拦截器(Interceptors)是实现org.apa ...

  4. 为什么vertical-align不起作用

    先看一段代码 <style> .title { margin:50px; color:blue; } .title span { font-size:24px; } .title span ...

  5. HttpClient 用于解决测试时候乱码的问题

    @Test public void doPostWithParam() throws Exception, IOException { CloseableHttpClient httpClient = ...

  6. @requestBody注解

  7. java包装类简析

    对于8个基本类型,java提供了他们相应的包装类: 基本类型 包装类 byte java.lang.Byte short java.lang.Short int java.lang.Integer l ...

  8. A glance at endpoint security

    Last year hackers stole millions from Taiwan First Commercial bank's ATMs without using a card. This ...

  9. 如何使用cmd打开磁盘目录和文件

    在windows操作系统中拥有命令行工具(cmd).cmd具有强大的功能,这里我就和大家介绍一下怎么使用cmd命令打开文件. 工具/原料 cmd命令行 打开cmd 1 在windows操作系统中按住w ...

  10. github 中删除仓库

    删除时,填充的名字是库的名字